Legacy Documentation: Version 4.6
Language: English
State Machine Basics
Blend Trees

Animation Parameters

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Animation Parameters are variables that are defined within the animation system but can also be accessed and assigned values from scripts. For example, the value of a parameter can be updated by an animation curve and then accessed from a script so that, say, the pitch of a sound effect can be varied as if it were a piece of animation. Likewise, a script can set parameter values to be picked up by Mecanim. For example, a script can set a parameter to control a Blend Tree.

Default parameter values can be set up using the Parameters widget in the bottom left corner of the Animator window. They can be of five basic types:

  • Int - an integer (whole number)
  • Float - a number with a fractional part
  • Bool - true or false value
  • Trigger - a boolean parameter that is reset by the controller when consumed by a transition.

Parameters can be assigned values from a script using functions in the Animator class: SetFloat, SetInt, and SetBool.

Here’s an example of a script that modifies parameters based on user input

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);
        }       
    }        
}


State Machine Basics
Blend Trees