控制人物動畫播放
這里我重新弄了一個簡單的場景和新的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的實例后通過腳本進行賦值的。