Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 5.x Animation Cookbook

You're reading from   Unity 5.x Animation Cookbook An advanced solution to all your Animation problems

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785883910
Length 328 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Maciej Szczesnik Maciej Szczesnik
Author Profile Icon Maciej Szczesnik
Maciej Szczesnik
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Working with Animations FREE CHAPTER 2. Working with the Animation View 3. 2D and User Interface Animation 4. Character Movement 5. Character Actions and Expressions 6. Handling Combat 7. Special Effects 8. Animating Cutscenes 9. Physics and Animations 10. Miscellaneous

Using parameters to control the animation flow

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.

Getting ready

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 IdleWave transition, you will be able to see the transition condition.

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.

How it works...

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 the Animator component object. It is reset by the Animator Controller after it is consumed (used) by a transition. The string name parameter of the function has to match your trigger parameter name set in the Animator 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 EqualGreaterLess, and Not Equal options to compare the value of your parameter with the given number. Integer type parameters are set with the SetInteger(string name, int value) function. The string name parameter needs to match the parameter name set in the controller. The int 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.

There's more...

  • You can add more than one condition to a state transition by clicking on the plus icon in the Conditions section in the transition Inspector. 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 the Inspector.
  • If you want to remove a transition, select it in the Inspector and click on the minus icon, or select it in the Animator 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 the Delete option from the context menu.
You have been reading a chapter from
Unity 5.x Animation Cookbook
Published in: May 2016
Publisher: Packt
ISBN-13: 9781785883910
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime