You can define a set of parameters in an Animator Controller and use them to control the transitions between animation states in Mecanim. In this recipe, we will show how to use parameters for transition conditions and use scripts to set values of those parameters in runtime.
Before we start, you should prepare an Animator Controller with at least one transition between animation states. The controller should be assigned to a character (its Animator component) placed in a scene. You can also use the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 05 Using parameters to control the animation flow
directory. You will find an Example.unity
scene there. There is a Warrior
game object in the scene's Hierarchy
. If you run the game and press the space bar, the Warrior
will make a wave gesture. You can select the Warrior
and open his Animator Controller
. If you click on the Idle
| Wave
transition, you will be able to see the transition condition.
To use parameters for controlling state transitions, follow these steps:
- Open the
Animator Controller
asset. - Find the
Parameters
tab in the upper left corner of theAnimator
window and click on it. - Click on the plus icon in the
Parameters
tab to add a new parameter. - Choose the
Trigger
type for the parameter. - Type a name of the newly created parameter (in the provided example, the name of the parameter is
Wave
). - Click on the transition between states you want to use parameters for. In the provided example, it is the transition between
Idle
andWave
animation states.Idle
is the default state. - Go to the
Inspector
tab and find theConditions
section.
- Click on the plus icon to add a new condition. If you have only one parameter, it will be chosen as the condition automatically. If you have more parameters, you need to choose the proper one from a drop-down list.
- If you want to make an immediate transition between your animation states, make sure to disable the
Has Exit Time
option, found above theSettings
foldout. - Your transition will take place only when its conditions are met. You need to set the parameter using scripts.
- To create a new C# script, click on the right mouse button in the
Project View
and selectCreate
|C# Script
. Name the script as you wish (in the provided example, it's calledWave
, the same as the parameter it sets). - Open the script and write the following:
using UnityEngine; using System.Collections; public class Wave : MonoBehaviour { //The anim variable is used to store the reference //to the Animator component of the character. private Animator anim; void Start () { //We get the component and assign it to //the anim variable when the game starts anim = GetComponent<Animator>(); } void Update () { //We check if player pressed the spacebar if (Input.GetKeyDown(KeyCode.Space)) { /*We cal the SetTrigger() function on the Animator component stored in the anim variable. The function requires one parameter - the name of the trigger parameter set in our Animator Controller ("Wave" in our example). Make sure to match it with the name of the parameter you've created in your Animator Controller*/ anim.SetTrigger("Wave"); } } }
- Make sure your class name is the same as the file name of the script, as it won't compile otherwise.
- Assign the script to the character, to the same
Transform
that has theAnimator
component with yourAnimator Controller
attached. Play the game and press the space bar; you should see your character switch to the next animation state.
You can use several types of parameters and corresponding script functions to set them:
- Trigger: This is the simplest parameter. It is set to true with the
SetTrigger(string name)
function called on theAnimator
component object. It is reset by theAnimator Controller
after it is consumed (used) by a transition. Thestring name
parameter of the function has to match your trigger parameter name set in theAnimator Controller
. - Int: This is an integer parameter. When you use it, you have to specify a logical comparison in the condition. The transition will only occur if the value of the parameter meets the comparison condition with a given number. You can use the
Equal
,Greater
,Less
, andNot Equal
options to compare the value of your parameter with the given number. Integer type parameters are set with theSetInteger(string name, int value)
function. Thestring name
parameter needs to match the parameter name set in the controller. Theint value
parameter is the value to set the controller parameter to. - Float: This is a float parameter. It works the same as the integer type, but uses floating point numbers instead of integers. It is set using the
SetFloat(string name, float value)
function. - Bool: This is a Boolean parameter. The condition can check if the parameter is true or false. The value of the parameter is set with the
SetBool(string name, bool value)
function.
- You can add more than one condition to a state transition by clicking on the plus icon in the
Conditions
section in the transitionInspector
. For the transition to occur, all its conditions have to be met. It works as logical AND for the conditions. - You can also create more than one transition between the same states. To do this, right-click on the state you want to transition from and choose the
Make Transition
option, and then select the state you already have a transition to. A multi-transition is marked with three arrows instead of one. If the conditions of any of the transitions are met, the transition will occur. You can use it as logical OR for transition conditions.
- If you have more than one transition between states, you can only edit one of them at a time. To edit a transition, select it in the
Transitions
section of theInspector
.
- If you want to remove a transition, select it in the
Inspector
and click on the minus icon, or select it in theAnimator Controller
and press Delete on the keyboard. Pressing Delete removes all the transitions.
- If you want to remove a condition from a transition, select it in the
Inspector
tab and click on the minus icon. To select a condition, you need to click on the handle to the left of the condition drop-down list (the handle looks like a = sign). - If you want to remove a parameter from the
Animator Controller
, you need to click on the handle on the left of the parameter and press the Delete button on the keyboard. You can also right-click on the parameter and choose theDelete
option from the context menu.