tip:transition 勾選Has Exit Time B動畫播放完畢后就可以自己返回A不用代碼控制。因為想做一個小人靜止時 隔兩秒會擺動小手的特效。
附上代碼參考:

1 using UnityEngine; 2 using System.Collections; 3 4 public class playeMove : MonoBehaviour 5 { 6 7 public Animator PlayerAnimator; 8 public const int HERO_UP = 0; 9 public const int HERO_RIGHT = 1; 10 public const int HERO_DOWN = 2; 11 public const int HERO_LEFT = 3; 12 float FreakTime=3; 13 //人物當前行走的方向狀態 14 public int state = 0; 15 //人物移動速度 16 public int moveSpeed =2; 17 18 //初始化人物位置 19 public void Awake() 20 { 21 state = HERO_UP; 22 } 23 // Use this for initialization 24 void Start() 25 { 26 27 } 28 29 // Update is called once per frame 30 void Update() 31 { 32 33 34 //獲取控制的方向, 上下左右, 35 float KeyVertical = Input.GetAxis("Vertical"); 36 float KeyHorizontal = Input.GetAxis("Horizontal"); 37 //Debug.Log("keyVertical" + KeyVertical); 38 //Debug.Log("keyHorizontal" + KeyHorizontal); 39 if (KeyVertical <0) 40 { 41 setHeroState(HERO_DOWN); 42 } 43 else if (KeyVertical >0) 44 { 45 setHeroState(HERO_UP); 46 } 47 48 if (KeyHorizontal >0) 49 { 50 setHeroState(HERO_RIGHT); 51 } 52 else if (KeyHorizontal <0) 53 { 54 setHeroState(HERO_LEFT); 55 } 56 57 58 59 //得到正在播放的動畫狀態 60 AnimatorStateInfo info = PlayerAnimator.GetCurrentAnimatorStateInfo(0); 61 62 //如果沒有按下方向鍵且狀態不為walk時播放走路動畫 63 if (KeyVertical != 0 || KeyHorizontal != 0 && !info.IsName("Walk")) 64 { 65 PlayerAnimator.Play("Walk"); 66 } 67 //否則如果按下方向鍵且狀態為walk時播放靜止動畫 68 else if((KeyVertical == 0 && KeyHorizontal == 0 && info.IsName("Walk") )) 69 { 70 PlayerAnimator.Play("Idle"); 71 } 72 73 //這里設定是玩家靜止時隔2s會擺動一次 74 if (KeyVertical == 0 && KeyHorizontal == 0) 75 { 76 //當玩家靜止時,FreakTime才會計時 77 if (info.IsName("Idle")) 78 { 79 FreakTime -= Time.deltaTime; 80 if (FreakTime <= 0) 81 { 82 Debug.Log(FreakTime); 83 FreakTime = 2; 84 //FreakingOut設置為播放后自動退出到idle 85 PlayerAnimator.Play("FreakingOut "); 86 } 87 } 88 } 89 90 91 } 92 93 94 void setHeroState(int newState) 95 { 96 //根據當前人物方向與上一次備份的方向計算出模型旋轉的角度 97 int rotateValue = (newState - state) * 90; 98 Vector3 transformValue = new Vector3(); 99 100 //播放行走動畫 101 102 //模型移動的位置數值 103 switch (newState) 104 { 105 case HERO_UP: 106 transformValue = Vector3.forward * Time.deltaTime; 107 break; 108 case HERO_DOWN: 109 transformValue = (-Vector3.forward) * Time.deltaTime; 110 break; 111 case HERO_LEFT: 112 transformValue = Vector3.left * Time.deltaTime; 113 break; 114 case HERO_RIGHT: 115 transformValue = (-Vector3.left) * Time.deltaTime; 116 break; 117 } 118 119 transform.Rotate(Vector3.up, rotateValue); 120 //移動人物 121 transform.Translate(transformValue * moveSpeed, Space.World); 122 state = newState; 123 } 124 125 126 } 127