StateMachineBehaviourの使い方

投稿者: | 2020-03-16

UnityのStateMachineBehaviourを使ってみました。

アニメーターコントローラーに、モーションの付いた2つのステートを追加して、背景を左クリックします。

インスペクタで、「Add Behaviour」をクリックしてスクリプトを新規追加します。

スクリプトをダブルクリックして編集します。

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

public class BaseLayer : StateMachineBehaviour
{
    // OnStateEnter is called before OnStateEnter is called on any state inside this state machine
    //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateExit is called before OnStateExit is called on any state inside this state machine
    //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateMove is called before OnStateMove is called on any state inside this state machine
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateIK is called before OnStateIK is called on any state inside this state machine
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateMachineEnter is called when entering a state machine via its Entry Node
    //override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
    //{
    //    
    //}

    // OnStateMachineExit is called when exiting a state machine via its Exit Node
    //override public void OnStateMachineExit(Animator animator, int stateMachinePathHash)
    //{
    //    
    //}
}

使う関数のコメントを外します。まずは「OnStateEnter」を使ってみます。
これは、新しいステートに移ったときに実行されます。

// OnStateEnter is called before OnStateEnter is called on any state inside this state machine
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
    Debug.Log(stateInfo.IsName("Armature|Action2"));
}

「stateInfo」に、エンターしたステートの情報が含まれています。
例えば、IsName()を使うと、ステートの名前が一致する時にTrueを返します。

「animator」でパラメーターなどの制御もできます。

「layerIndex」には、このスクリプトが付いているレイヤーの番号が入っています。

// OnStateEnter is called before OnStateEnter is called on any state inside this state machine
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
    //Debug.Log(stateInfo.IsName("Armature|Action2"));
    Debug.Log(layerIndex);
}

Baseレイヤーなのでここでは0です。

「OnStateUpdate」はフレームごとに実行されるのだと思います。

// OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
    Debug.Log(Time.deltaTime);
}

オブジェクトをアタッチする

publicや[SerializeField]属性も使えますが、ヒエラルキーにあるオブジェクトはアタッチできず、プレハブなら使えます。

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

public class BaseLayer : StateMachineBehaviour
{
    public GameObject cube;

    Vector3 p = Vector3.zero;
    Vector3 q = Vector3.zero;
    GameObject c;

    // OnStateEnter is called before OnStateEnter is called on any state inside this state machine
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        //Debug.Log(stateInfo.IsName("Armature|Action2"));
        //Debug.Log(layerIndex);

        c = Instantiate(cube, p, Quaternion.Euler(Vector3.zero));
        p.x -= 3f;
        p.y += 2f;

    }

    // OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        q = c.transform.position;
        q.z += 0.1f;
        c.transform.position = q;
    }

// ...

Cubeのプレハブをアタッチして、インスタンスを作り、それを変数にいれてOnStateUpdateで操作することもできました。

コメントを残す

メールアドレスが公開されることはありません。