Tejas Jasani, founder the Mobile Development company The APP Guruz authors an look at how to play multiple animations on a character in the Unity Game Engine, covering and explaining all the components and what they do.

Objective

In unity, Animator Controller gives the facility to play one or more animations on an object. The main objective of this blog is to explain how to play one or more animation on an object or character.

 

Unity Animator Controller

The Animator Controller is the main component by which animation behavior is added to an object.

To create animator controller in Project View, go to Create -> Animator Controller that creates a .controller asset. Its elements like parameters, layers and states can be set from Animator Controller View (Menu: Window > Animator Controller).

The animator controller window will contain,

  • The Animation Layer Window
  • The Event Parameters Window 
  • The visualization of State Machine

 

Animation Layer

Animation Layers allow grouping and prioritizing the weighting of animations. Animation Layers are used to manage complex state machines for different body parts.

An example of this is if we have a lower-body layer for walking-jumping, and an upper-body layer for throwing objects / shooting.

Animation layers can be managed from the Layers Widget in the top-left corner of the Animator Controller window. By clicking on + button, new layer can be added and for each layer, we can set following properties,

  • Name: Name of the layer.
  • Weight: Weight (or priority) of the animation.
  • Mask: It specifies the body mask that is used in this layer. Body mask is the part of the body on which animation would be applied.
  • Blending: Set the blending type (either Override or Addictive). Override will ignore the information from other layers, while Addictive will add animation on top of previous layers.
  • Sync: Used for synchronizing the layers. 
  • IK Pass: It determines will IK curves will be included or not in animation blending.

 

Animation Parameters

Animation Parameters are one type of variables that can be created or defined in the Event Parameter Widget in the Animator Controller Window.  Default value of parameters is set in the Event Parameter Widget but it can be accessed and modified from scripts.

There are following basic type of parameters,

  • Float: A fractional number 
  • Int: An Integer number
  • Bool: True or false value

 

Animation State Machines

Generally in the game, single character can have more than one animation behaviors to show different actions. For example, initially player has walk animation but after increasing some speed, it has run animation and by clicking on jump button it would have jump animation. State machine simplifies the control and sequence of all these animations on single character.

 

State Machine has following elements

 

Animation States:

Animation State is an element of Animation State Machine that represents an individual animation. We can define transitions between two animation states that determine when it should move from one animation state to another.

Animation State has following properties,

Speed: It defines the default speed of the animation

Motion: It defines the animation clip that is assigned to this state. 

Foot IK: It determines whether Foot IK be respected for this state or not? 

Transitions: It defines the list of transition that is originated from this state.

 

Animation Transition:

Animation Transitions is the part of the Animation State Machine that are used to transition smoothly from one animation state to another. An animation state can have multiple animation transitions but only one transition can be active at a time.

 

Animation Transition has following properties

Atomic: It defines whether this transition atomic (cannot be interrupted) or not.

Conditions: It defines when transitions will be triggered.

 

Blend Trees:

A blend tree is a special type of animation node that allows multiple different animation clips to be played at the same time so that an animated object can shift between different motions. Sometimes animation would appear jerky because of quick swapping of animation in animation transition. To avoid this and to give smooth effect when animations are swapped, blend tree is used.

Selecting Right-Click-> Create State -> From New Blend Tree option in Animator Controller Window can create new blend tree. It will look like simple animation state but Motion value of blend tree is set to Blend Tree; that means multiple animations can be hold by blend tree.

Unity-Blend-Trees

Rename the blend tree to Stickman_Movement.  Double click on  Stickman_Movement, new grid will be appeared.

 

Unity-Blend-Tree-2

This is the ‘inside’ of blend tree and where animations can be setting up for blending. In the Inspector, set the Blend Type to 1D. There is other three-blend types i.e. 2D Simple Directional, 2D Freeform Directional, 2D Freeform Cartesian.

Now create a parameter that can be used to manage blending by blend tree. Create new parameter of type Float in Parameter Widget and name it StickmanSpeed. Back in the Inspector, click on the Parameter dropdown and select StickmanSpeed as blending parameter. Now the value of the StickmanSpeed parameter will control the blend of animations.

Next we need to add some actual motions to our blend tree. In the Inspector, click the + sign below the Motion field and select Add Motion Field to add a new blank motion. Now select the motion (animation clip) which we want to set (here WalkAnimation added) and add another two motions RunAnimation and JumpAnimation.

Unity-Animator-Component

 

Animator Component

Animator Controller can be accessed using Animator Component. Animator is the Component, which references Animator Controller for setting up the behavior of a character.  Following are the properties of the Animator,

  • Controller: The animator controller attached to this character or Animator.
  • Avatar: The Avatar for this character.
  • Apply Root Motion: Should we control the character’s position from the animation itself or from script?
  • Animate Physics: Should the animation interact with physics?
  • Culling Mode: Culling mode for animation.
  • Always animate: Always animate, don’t do culling
  • Based On Renderers: At the point when the renderers are imperceptible, just root motion is animated. All other body parts will stay static while the character is invisible.

Example

The following simple c# code will just change the animation parameter runtime and as per that parameter’s value, animation of stickman will be played.

Note

  • Add empty GameObject in scene and add SpriteRenderer and Animator components on it.
  • Add following script on this empty GameObject.
  • Create Animator Controller and create two Animation States (WalkAnimation and RunAnimation) in Animator Controller View.
  • Add new Animation Parameter of type float in Animation Parameter Widget.
  • Make transition and set its conditions property.

AnimatorControllerScript

using UnityEngine;
using System.Collections;

public class AnimatorControllerScript : MonoBehaviour
{
private Animator animator;
private float speed;

void Start ()
{
animator = gameObject.GetComponent<Animator>();
StartCoroutine(setSpeed());
}

//Following enumerator just change the value of Animation Parameter randomly
IEnumerator setSpeed()
{
yield return new WaitForSeconds(1.0f);
speed = Random.Range(0,3.0f);
//This line set the value of Animation Parameter 
animator.SetFloat(“StickmanSpeed”,speed);
StartCoroutine(setSpeed());
}
}

 

Author Bio

Tejas Jasani is a founder of a Mobile Development company named The APP Guruz, His major focus is on how to improve mobile users smart phone experience through development of mobile games and apps.