天天看點

unity之Mecanim動畫系統學習(4):動畫播放邏輯unity之Mecanim動畫系統學習(4)

unity之Mecanim動畫系統學習(4)

人物動畫控制播放

unity之Mecanim動畫系統學習(4):動畫播放邏輯unity之Mecanim動畫系統學習(4)

以上動畫包括靜立/向前走/向左走/向右走/向後走/向前奔跑,通過箭頭來進行狀态過渡

unity之Mecanim動畫系統學習(4):動畫播放邏輯unity之Mecanim動畫系統學習(4)

并且,可以通過設定參數來控制動畫什麼時候進入下一狀态,動畫的播放參數等

比如下面一段比較笨的代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterMove : MonoBehaviour
{
    private Animator animator;
    private bool LeftShift = false;

    private void Start()
    {
        animator = this.GetComponent<Animator>();

    }

    private void Update()
    {
        Controll();
    } 
    public void Controll()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            animator.SetBool("walk_front",true);
        }
        if(Input.GetKeyUp(KeyCode.W))
        {
            animator.SetBool("walk_front",false);
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            animator.SetBool("walk_left", true);
        }
        if (Input.GetKeyUp(KeyCode.A))
        {
            animator.SetBool("walk_left", false);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            animator.SetBool("walk_right", true);
        }
        if (Input.GetKeyUp(KeyCode.D))
        {
            animator.SetBool("walk_right", false);
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            animator.SetBool("walk_back", true);
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            animator.SetBool("walk_back", false);
        }

        if (LeftShift==true  )
        {
            animator.SetBool("run",true);
            Debug.Log("unity");
        }
        if ( LeftShift == false )
        {
            animator.SetBool("run",false);
        }
        if(Input.GetKeyDown(KeyCode.LeftShift))
        {
            LeftShift = true;
            Debug.Log(LeftShift);
        }
        if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            LeftShift = false ;
            Debug.Log(LeftShift);
        }
        if(Input.GetKeyDown(KeyCode.J))
        {
            Debug.Log("j");
        }
        if (Input.GetKeyDown(KeyCode.J)&&Input .GetKeyDown(KeyCode.K))
        {
            Debug.Log("jk");
        }
    }
    
}
           
unity之Mecanim動畫系統學習(4):動畫播放邏輯unity之Mecanim動畫系統學習(4)

通過這些代碼,就能夠實作鍵盤對人物的前後左右移動

State Machine Behavior

我們也可以通過添加State Machine Behavior增加行為

unity之Mecanim動畫系統學習(4):動畫播放邏輯unity之Mecanim動畫系統學習(4)

以下是中文翻譯後的代碼注釋

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class walk_front : StateMachineBehaviour
{
    //OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        //進入目前狀态時會被調用
    }

    //OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        //進入目前狀态的每幀會被調用
    }

    //OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        //退出目前狀态時會被調用
    }

    //OnStateMove is called right after Animator.OnAnimatorMove()
    override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        
    }

    //OnStateIK is called right after Animator.OnAnimatorIK()
    override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        
    }
}
           

通過使用State Machine Behaviour我們可以更加友善的在特定的時間點觸發一些我們需要的事件,但是需要注意的是,我們一般給State Machine Behaviour指派一些場景的對象不是直接在Inspector面闆裡拖拽而是通過Animator的GetBehavior方法擷取到指定的State Machine Behaviour的執行個體後通過腳本進行指派的(這有别于平時的C#代碼,當然,這樣做其實代碼執行效率會高一些)。

繼續閱讀