LoginSignup
32
25

More than 5 years have passed since last update.

Unity C# Animatorを扱う時のなにがし

Posted at

AnimatorのParameterを操作

詳しくは 公式リファレンス を参照するとよいけど、
以下を覚えておくだけでも結構いける。

using UnityEngine;

public class Sample : MonoBehaviour {

    [SerializeField]
    Animator mAnimator; // InspectorでAnimatorを指定しておく

    void Start () {

        mAnimator.SetBool("Hoge", true); // HogeというBool値パラメーターをtrue

        mAnimator.SetTrigger("goHoge"); // goHogeというTriggerをon

        mAnimator.SetInteger("HogeRandom", 1); // HogeRandomというInt値パラメーターを1
    }
}

現在実行中のアニメーション名を確認

どのアニメーションを実行しているかで処理を変えたい場合。

using UnityEngine;

public class Sample : MonoBehaviour {

    [SerializeField]
    Animator mAnimator; // InspectorでAnimatorを指定しておく

    void Update () {

        AnimatorStateInfo stateInfo = mAnimator.GetCurrentAnimatorStateInfo(0);
        // レイヤーのインデックスを引数に指定

        if( stateInfo.IsName("Base.foo") ) {

            Debug.Log("Baseレイヤーのfooを実行中");
        }
    }
}

AnimatorStateInfoの詳しい機能はここを参照!

32
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
25