How to do it...

To use parameters for controlling state transitions, follow these steps:

  1. Open the Animator Controller asset.
  2. Find the Parameters tab in the upper left corner of the Animator window and click on it.
  3. Click on the plus icon in the Parameters tab to add a new parameter.
  4. Choose the Trigger type for the parameter.
  5. Type a name of the newly created parameter (in the provided example, the name of the parameter is Wave).
  6. 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.
  7. Go to the Inspector tab and find the Conditions section.
  1. 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.
  1. 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.
  2. Your transition will take place only when its conditions are met. You need to set the parameter using scripts.
  3. To create a new C# script, click on the right mouse button in the Project View and select CreateC# Script. Name the script as you wish (in the provided example, it's called Wave, the same as the parameter it sets).
  4. 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"); 
      } 
      } 
      } 
  1. Make sure your class name is the same as the file name of the script, as it won't compile otherwise.
  2. 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.7.102