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 the Animator 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 and Wave animation states. Idle is the default state.
- Go to the Inspector tab and find the Conditions 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 the Settings 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 select Create | C# Script. Name the script as you wish (in the provided example, it's called Wave, 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 the Animator component with your Animator Controller attached. Play the game and press the space bar; you should see your character switch to the next animation state.