控制人物动画播放
这里我重新弄了一个简单的场景和新的Animator Controller来作为示例。
下面先看看Animator Controller的配置:
人物在站立状态只能进入走路,走路只能进入奔跑或返回站立,奔跑只能进入跳跃或返回走路,跳跃则只能返回奔跑。
参数方面为两个参数:
- Float类型的moveSpeed;
- Trigger类型的Jump;
连线直接的转换条件为:moveSpeed大于0.1进入走路,走路moveSpeed大于0.9进入奔跑小于0.1返回站立,奔跑moveSpeed小于0.9返回走路,Jump被触发则进入跳跃;
为人物添加Animator组件并绑定上面的Animator Controller文件,同时绑定下面的脚本:
1 using UnityEngine; 2 using System.Collections; 3 4 public class TestAnim : MonoBehaviour 5 { 6 //将名称转换为哈希值可以提高索引的速度 7 private int moveSpeed = Animator.StringToHash("moveSpeed"); 8 private int jump = Animator.StringToHash("Jump"); 9 private int runState = Animator.StringToHash("Base Layer.Run"); 10 11 private Animator _animator; 12 13 void Start() 14 { 15 _animator = this.GetComponent<Animator>(); 16 } 17 18 void Update() 19 { 20 float speed = Input.GetAxis("Vertical"); 21 _animator.SetFloat(moveSpeed, speed); 22 23 //获取动画的当前状态 24 AnimatorStateInfo info = _animator.GetCurrentAnimatorStateInfo(0); 25 //跳跃除了要按下空格键外, 还需要处于奔跑状态才行, 否则按下空格键时就会标记 Jump, 一进入奔跑就马上跳跃 26 if(Input.GetKeyDown(KeyCode.Space) && info.fullPathHash == runState) 27 { 28 _animator.SetTrigger(jump); 29 } 30 } 31 }
State Machine Behavior
在Unity5.0中,我们可以给Animator Controller中的每个State添加脚本了,类似于专门用于GameObject的MonoBehavior,State可以添加State Machine Behavior,我们打开任意的Animator Controller,就可以在Inspector窗口发现添加脚本的按钮:
除了State外,我们还可以在层上添加,层可以看做包含了整个State的一个大State:
我们可以添加一个脚本看看,下面是我添加了注释的State Machine Behavior脚本的标准形式:
1 using UnityEngine; 2 using System.Collections; 3 4 public class StateScript : StateMachineBehaviour 5 { 6 // OnStateEnter is called before OnStateEnter is called on any state inside this state machine 7 override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 8 { 9 //进入当前状态时会被调用 10 } 11 12 // OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine 13 override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 14 { 15 //进入当前状态的每帧会被调用 16 } 17 18 // OnStateExit is called before OnStateExit is called on any state inside this state machine 19 override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 20 { 21 //退出当前状态时会被调用 22 } 23 24 // OnStateMove is called before OnStateMove is called on any state inside this state machine 25 override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 26 { 27 } 28 29 // OnStateIK is called before OnStateIK is called on any state inside this state machine 30 override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 31 { 32 } 33 34 // OnStateMachineEnter is called when entering a statemachine via its Entry Node 35 override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash) 36 { 37 } 38 39 // OnStateMachineExit is called when exiting a statemachine via its Exit Node 40 override public void OnStateMachineExit(Animator animator, int stateMachinePathHash) 41 { 42 } 43 }
通过使用State Machine Behaviour我们可以更加方便的在特定的时间点触发一些我们需要的事件,但是需要注意的是,我们一般给State Machine Behaviour赋值一些场景的对象不是直接在Inspector面板里拖拽而是通过Animator的GetBehavior方法获取到指定的State Machine Behaviour的实例后通过脚本进行赋值的。