1.Unity3D StateMachine正常的狀態下是每個動做結束后沒有回調,所以有時候我們要判斷當前不是哪一個動作是要做什么的,如下:
public class TestJS : MonoBehaviour { public Animator _animator; PRivate AnimatorTransitionInfo _changeAnimator; void Update () { _changeAnimator = _animator.GetAnimatorTransitionInfo(0); if (!_changeAnimator.IsName("xxx")) { //Do Sth } }}2.這時我們就可以優化:對他們每次,進入結束弄一個回調=>狀態改變:
在AnimationStateMachine里面寫結束,進入的回調。
3.這些回調還沒有被初始化,此時在應用這個AnimatorControl的物體上添加腳本進行初始化:
using UnityEngine;using System.Collections;public class ParkourPlayerAnimatorController : MonoBehaviour{ //引用 private ParkourPlayerAnimationHash animHash; private int random; public Animator flyRobot; [SerializeField] private Animator playerAnimator; public ParkourAnimationStates CurrentAnimationState { get; private set; } // Use this for initialization void Awake() { random = Random.Range(0,3); animHash = GetComponent<ParkourPlayerAnimationHash>(); //最重要的在這邊初始化回調函數因為結束回調暫時還可以省略,所以只是初始化進入某個動畫的回調: AnimationStateMachine[] _animStateMachine = playerAnimator.GetBehaviours<AnimationStateMachine>(); for (int i = 0; i < _animStateMachine.Length; i++) { _animStateMachine[i].EnterStateCallback = SetCurrentAnimationState; } } // Update is called once per frame void Update() { } //可以保存一個CurrentAnimationState 的狀態而不用像1.在Update一直判斷。 void SetCurrentAnimationState(int hashName) { if (hashName == animHash.RunState) { CurrentAnimationState = ParkourAnimationStates.Run; } else if (hashName == animHash.RollState) { CurrentAnimationState = ParkourAnimationStates.Roll; } else if (hashName == animHash.JumpState || hashName == animHash.JumpState1 || hashName == animHash.JumpState2) { CurrentAnimationState = ParkourAnimationStates.Jump; } else if (hashName == animHash.MoveSideState) { CurrentAnimationState = ParkourAnimationStates.MoveSide; } else if (hashName == animHash.FallState) { CurrentAnimationState = ParkourAnimationStates.Fall; } else if (hashName == animHash.FlyTrigger) { CurrentAnimationState = ParkourAnimationStates.Fly; } } public void Run() { playerAnimator.SetBool(animHash.RunBool, true); } public void Stop() { playerAnimator.SetBool(animHash.RunBool, false); } public void PlayJump() { switch (random % 3) { case 0: playerAnimator.SetTrigger(animHash.JumpTrigger); break; case 1: playerAnimator.SetTrigger(animHash.JumpTrigger1); break; case 2: playerAnimator.SetTrigger(animHash.JumpTrigger2); break; } random++; } public void PlayRoll() { playerAnimator.SetTrigger(animHash.RollTrigger); } public void PlayMoveSideLeft() { playerAnimator.SetTrigger(animHash.MoveSideLeftTrigger); } public void PlayMoveSideRight() { playerAnimator.SetTrigger(animHash.MoveSideRightTrigger); } public void PlayFly() { playerAnimator.SetTrigger(animHash.FlyTrigger); } public void PlayFall() { playerAnimator.SetTrigger(animHash.FallTrigger); } public void PlayCatchRob() { playerAnimator.SetTrigger("Catch"); } public void PlayFlyRobot() { playerAnimator.SetTrigger(animHash.FlyTrigger); }}在3中有一個引用到animHash判斷每個動畫SetCurrentAnimationState(int hashName)是否相等,來記錄using UnityEngine;using System.Collections;public class ParkourPlayerAnimationHash : MonoBehaviour{ public int IdleState { get; private set; } public int RunState { get; private set; } public int JumpState { get; private set; } public int JumpState1 { get; private set; } public int JumpState2 { get; private set; } public int MoveSideState { get; private set; } public int RollState { get; private set; } public int FallState { get; private set; } public int RunBool { get; private set; } public int JumpTrigger { get; private set; } public int JumpTrigger1 { get; private set;} public int JumpTrigger2 { get; private set;} public int RollTrigger { get; private set; } public int FallTrigger { get; private set; } public int FlyTrigger { get; private set; } public int MoveSideLeftTrigger { get; private set; } public int MoveSideRightTrigger { get; private set; } // Use this for initialization void Awake() { IdleState = Animator.StringToHash("Base Layer.待機"); RunState = Animator.StringToHash("Base Layer.跑步"); JumpState = Animator.StringToHash("Base Layer.跳"); JumpState1 = Animator.StringToHash("Base Layer.跳1"); JumpState2 = Animator.StringToHash("Base Layer.跳2"); MoveSideState = Animator.StringToHash("Base Layer.側移"); RollState = Animator.StringToHash("Base Layer.下滑"); FallState = Animator.StringToHash("Base Layer.摔倒"); FlyTrigger = Animator.StringToHash("Fly"); RunBool = Animator.StringToHash("Run"); JumpTrigger = Animator.StringToHash("Jump"); JumpTrigger1 = Animator.StringToHash("Jump1"); JumpTrigger2 = Animator.StringToHash("Jump2"); RollTrigger = Animator.StringToHash("Roll"); FallTrigger = Animator.StringToHash("Fall"); MoveSideLeftTrigger = Animator.StringToHash("MoveSideLeft"); MoveSideRightTrigger = Animator.StringToHash("MoveSideRight"); }}
All our dreams can come true, if we have thecourage to pursue them. 我們所有的夢想都可以成真,只要我們有勇氣去追求它們。 ————沃爾特·迪斯尼
新聞熱點
疑難解答