標籤:tar orm 大神 stat value 播放 blog time view
人物轉向移動的代碼是我在網上粘貼後改動的-----------------------侵刪---------------------------
痛點:如何在A動畫中插播B動畫再返回
思路:開始我的想法是在兩個動畫之間create transition用代碼控制返回,但是條件是2s播放一次,間隔時間的重製和這個有衝突,如何使B動畫播放完畢後再返回,後來經大神指點(萬分感謝呀O(∩_∩)O~~),在A動畫播放的狀態下判斷時間,2s後直接播放B動畫,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 View Code
控制人物轉向移動,動畫播放的過程中插播其他動畫後返回