ステートマシンの基本
ブレンドツリー

アニメーション パラメータ

アニメーションパラメータはアニメーションシステム内で定義されているだけでなく,アクセスでき,スクリプトから値を割り当てることができる変数です。例えば,パラメータの値が メカニムのアニメーションカーブ によって更新が行え,その時スクリプトからアクセス,例えば,サウンドエフェクトのピッチを変更させることが出来ます。同様に,スクリプトはMecanimによってピックアップされるパラメータの値も設定することが出来ます。例えば,スクリプトは Blend Tree をコントロールするパラメータを設定することが出来ます。

デフォルトのパラメータ値は,アニメーターウィンドウの左下にあるパラメータウィジェットを使用して設定することが出来ます。基本的には5つのパラメータがあります。

  • Int - 整数
  • Float - 小数部分を持つ数値
  • Bool - trueまたはfalseの値
  • Trigger - 遷移によって消費された時,コントローラによってリセットされるbooleanパラメータ

パラメータはアニメータークラスの関数を使用してスクリプトから値を割り当てることが出来ます: SetFloat, SetInt, SetBool.

ここで,ユーザ入力に基づいてパラメータを修正スクリプトの例をお見せします。

using UnityEngine;
using System.Collections;


public class AvatarCtrl : MonoBehaviour {

    protected Animator animator;
    
    public float DirectionDampTime = .25f;
    
    void Start () 
    {
        animator = GetComponent<Animator>();
    }
    
    void Update () 
    {
        if(animator)
        {
            //get the current state
            AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
            
            //if we're in "Run" mode, respond to input for jump, and set the Jump parameter accordingly. 
            if(stateInfo.nameHash == Animator.StringToHash("Base Layer.RunBT"))
            {
                if(Input.GetButton("Fire1")) 
                    animator.SetBool("Jump", true );
            }
            else
            {
                animator.SetBool("Jump", false);                
            }
            
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            
            //set event parameters based on user input
            animator.SetFloat("Speed", h*h+v*v);
            animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime);
        }       
    }        
}


ステートマシンの基本
ブレンドツリー