一、有限状态机FSM概述

二、不用有限状态机实现角色的走路、攻击动作

(1)新建角色、地面、为角色添加动画


(2)为角色添加脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Player1Control : MonoBehaviour
{private Animator ani;void Start(){ani = GetComponent<Animator>();   }// Update is called once per framevoid Update(){float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal");Vector3 dir = new Vector3(horizontal, 0, vertical);if (dir != Vector3.zero){transform.rotation = Quaternion.LookRotation(dir);transform.Translate(Vector3.forward * 3 * Time.deltaTime);ani.SetBool("IsWalk", true);}else{ani.SetBool("IsWalk", false);}if (Input.GetKeyDown(KeyCode.Space)){ani.SetTrigger("Attack");}}
}

这样是能够实现角色的各种动画,但是由于if语句过多,可能以后修改起来会很麻烦。

三、用方法实现角色的走路、攻击动作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public enum PlayerState
{idle,walk,attack
}
public class Player2Control2 : MonoBehaviour
{private Animator ani;private PlayerState state = PlayerState.idle;// Start is called before the first frame updatevoid Start(){ani = GetComponent<Animator>();}// Update is called once per framevoid Update(){switch (state){case PlayerState.idle:idle();break;case PlayerState.walk:walk();break;case PlayerState.attack:attack();break;}}void idle(){//站立方法ani.SetBool("IsWalk", false);float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal");Vector3 dir = new Vector3(horizontal, 0, vertical);if (dir != Vector3.zero){//切换为走路状态state = PlayerState.walk;}if (Input.GetKeyDown(KeyCode.Space)){//切换为攻击状态state = PlayerState.attack;}}void walk(){ani.SetBool("IsWalk", true);float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal");Vector3 dir = new Vector3(horizontal, 0, vertical);if (dir != Vector3.zero){transform.rotation = Quaternion.LookRotation(dir);transform.Translate(Vector3.forward * 3 * Time.deltaTime);}else{state = PlayerState.idle;}}void attack(){ani.SetTrigger("Attack");//如果你当前的状态不是攻击,切换状态if (!ani.GetCurrentAnimatorStateInfo(0).IsName("Elemental@UnarmedAttack01")) ;{state = PlayerState.idle;}}
}

三、用类实现角色的走路、攻击动作

状态类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public enum PlayerState
{idle,walk,attack
}
public abstract class FSMstate
{//当前状态IDpublic int StateID;//当前状态拥有者public MonoBehaviour mono;//状态所属管理器public FSMManager fSMManager;public FSMstate(int StateID,MonoBehaviour mono,FSMManager manager){this.StateID = StateID;this.mono = mono;this.fSMManager = manager;}public abstract void OnEnter();public abstract void OnUpdate();
}

状态管理类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FSMManager : MonoBehaviour
{// Start is called before the first frame update//列表状态public List<FSMstate> stateList = new List<FSMstate>();//当前状态public int CurrentIndex = -1;public void ChangeState(int StateID){stateList[CurrentIndex].OnEnter();CurrentIndex = StateID;}//更新public void Update(){if (CurrentIndex != -1){stateList[CurrentIndex].OnUpdate();}}
}

PlayerControl类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Payer : MonoBehaviour
{private FSMManager fSMManager;void Start(){fSMManager = new FSMManager();IdleState idle = new IdleState(0,this,fSMManager);WalkState walk = new WalkState(1, this, fSMManager);AttackState attack = new AttackState(2, this, fSMManager);fSMManager.stateList.Add(idle);fSMManager.stateList.Add(walk);fSMManager.stateList.Add(attack);fSMManager.ChangeState((int)PlayerState.idle);}void Update(){fSMManager.Update();}
}

IdleState类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class IdleState : FSMState
{public IdleState(int stateID,MonoBehaviour mono,FSMManager manager) : base(stateID, mono, manager){}public override void OnEnter(){mono.GetComponent<Animator>().SetBool("IsWalk", false);}public override void OnUpdate(){float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal");Vector3 dir = new Vector3(horizontal, 0, vertical);if (dir != Vector3.zero){fSMManager.ChangeState((int)PlayerState.walk);}//监听攻击状态切换if (Input.GetKeyDown(KeyCode.Space)){fSMManager.ChangeState((int)PlayerState.attack);}}}

WalkState类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class WalkState : FSMState
{public WalkState(int stateID,MonoBehaviour mono,FSMManager manager) : base(stateID, mono, manager){}public override void OnEnter(){mono.GetComponent<Animator>().SetBool("IsWalk", true);}public override void OnUpdate(){float horizontal = Input.GetAxis("Horizontal");float vertical = Input.GetAxis("Vertical");Vector3 dir = new Vector3(horizontal, 0, vertical);if (dir != Vector3.zero){mono.transform.rotation = Quaternion.LookRotation(dir);mono.transform.Translate(Vector3.forward * 3 * Time.deltaTime);}else{//切换到站立状态fSMManager.ChangeState((int)PlayerState.idle);}}// Start is called before the first frame update}

AttackState类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class AttackState : FSMState
{public AttackState(int stateID,MonoBehaviour mono,FSMManager manager) : base(stateID, mono, manager){}public override void OnEnter(){mono.GetComponent<Animator>().SetTrigger("Attack");}public override void OnUpdate(){if (!mono.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Elemental@UnarmedAttack01")){fSMManager.ChangeState((int)PlayerState.idle);}}
}

Unity进阶第三章-有限状态机FSM(三种方法实现角色动画操作)相关推荐

  1. Javascript第三章循环最后一种方法for..in与for区别第二课

    for in 主要就是遍历对象 如果索引是字符串的形式,不能用for遍历,因为length识别不了他的长度 可以有for-in的方法 主要对象进行遍历 for in 遍历对象属性 获取的是对象的属性名 ...

  2. Java学习 第三章 数组(三)排序算法

    ** Java学习 第三章 数组(三)排序算法 ** 主要内容:排序算法.排序算法横向比较.Arrays工具类的使用.数组常见异常 1.数组中涉及到的常见算法:排序算法 1.1 排序算法分类:内部排序 ...

  3. 《Java语言程序设计与数据结构》编程练习答案(第三章)(三)

    <Java语言程序设计与数据结构>编程练习答案(第三章)(三) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  4. 计算机中的无线网卡使用哪两种类型的扩展槽,第三章计算机每一种扩展槽的相关信息.ppt...

    第三章计算机每一种扩展槽的相关信息 Product Department Zhang Yun Yun 2013.NOV 第三章 扩展槽 入琵商要篱邹囱驮飞长洞女阳弄驳成短椰此吴撤踌订览骄驴峰记号惰臻贝 ...

  5. vpwm的控制变频_第三章 VVF控制与PWM方法.ppt

    第三章 VVF控制与PWM方法 第三章 VVVF控制与PWM方法 3.1 VVVF变频调速原理 控制方法特点: 2.这是在忽略定子漏阻抗的影响得到的,在频率比较低时,这种忽略会带来偏小,电机力矩不够, ...

  6. 一元三次多项式因式分解的两种方法

    参考文献: 张育波. 一元三次多项式因式分解的两种方法[J]. 初中数学教与学, 2007, No.160(04):42.

  7. 怎么给视频配音?短视频作者在用的三种方法,简单易操作

    怎么给视频配音?短视频作者在用的三种方法,简单易操作 如果你也是做短视频的作者,那么相信你也一定知道,做短视频并不仅仅只是简单的拍摄剪辑发布而已,其中要做的工作还有很多,比如配音就是其中的一项.虽然并 ...

  8. 软件设计模式:三个类别,23种方法

    软件设计模式 软件设计模式(Design pattern),又称设计模式,是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证 ...

  9. SRPG游戏开发(六)第三章 绘制地图 - 三 创建自己的SrpgTile

    返回目录 第三章 绘制地图 一        导入素材 http://blog.csdn.net/darkrabbit/article/details/79168225 二        绘制一张简单 ...

最新文章

  1. 捡垃圾、跳大绳、种花、写字,波士顿动力机器狗迎来重大升级
  2. low逼三人组、nb二人组、归并、希尔排序----小结
  3. gin获取路径中的参数
  4. Numpy.tile() (Python)
  5. VTK:PolyData之ImplicitPolyDataDistance
  6. Vue学习笔记(二) —— 组件开发
  7. simhash与Google的网页去重(转)
  8. mysql学习【第2篇】:基本操作和存储引擎
  9. Ubuntu 13.04 安装最新版本的Nginx
  10. mysql数据库创建交叉表查询_sql – 我需要知道如何创建交叉表查询
  11. 【转贴】使用和制作patch文件
  12. 当出现opencv的.dll无法找到的问题的时候用一下处理方法
  13. 计算机专业色弱限制,体检标准变成建议 色盲色弱能报高校计算机专业
  14. 基于粒子群算法的PID控制器优化设计
  15. 《SEM长尾搜索营销策略解密》一一1.1 做有个性的账户
  16. CSS盒子模型——标准模型和IE模型的区别,如何设置标准模型、IE模型
  17. TortoiseSVN—Repo-browser,打开你要比较的两个版本所在的地址,选择一个版本做为比较的基础(单击右键—选择mark for comparison),再选择另外一个版本(单击右键—选
  18. Unity之EditorUtility-ProgressBar进度条-一
  19. miui11升级鸿蒙,MIUI11新版本推送 GPU驱动更新 小米10 Pro跑分轻松突破60万
  20. 用 M1 MacBook 当主力开发机:程序员使用半个月后如是说

热门文章

  1. 07-CBAM_block注意力机制
  2. 浙江工业大学2023考研计算机学硕经验贴
  3. 对网站文章标题的几点建议
  4. word2016怎么显示出修改位置 如何查看修改内容
  5. win10iis网站服务器,win10iis网站配置
  6. 下载vue-router
  7. XMR新算法RandomX设计原理
  8. MySQL的排序规则COLLATE
  9. SeetaFace 开源的人脸识别代码
  10. Python实现2ASK,2FSK和2PSK调制与解调