Animator Controllers are state machines (graphs) responsible for controlling the flow of animations of any animated object in the game. The same Animator Controller asset can be used by multiple objects or characters. Unity will create an independent runtime copy of the asset for each animated object it is assigned to.
As always, you should have a rigged and animated character ready before we start. Import it into Unity, choose the proper rig type, and put it into a scene. You can download the example Unity project and go to the Chapter 01 Working with animations\Recipe 03 Creating and assigning an animator controller
directory. There is a scene called Example.unity
there. If you open it, you'll find a Sheep character in the Hierarchy
. It has an Animator Controller already created and assigned. You can also use the Quadruped.fbx
file from the Chapter 01 Working with animations\Recipe 03 Creating and assigning an animator controller\Rigs
directory to follow the recipe step by step.
To create and assign an Animator Controller, follow these steps:
- Navigate to the
Project View
(any directory in theAssets
folder) and press the right mouse button. - Choose
Create
|Animator Controller
from the menu. A controller asset will be created. You can name it as you wish. - Double-click on the created controller. An
Animator
tab will appear. It will show the current selectedAnimator Controller
.
- Here you can add the first animation. Navigate to your imported character in the
Project View
. Unfold it and drag and drop one of the imported animations into theAnimator
window. A new state will be created and will be colored orange, showing that this is the default animation state—the state from which your graph starts.
- Navigate to your character on the scene and select it.
- Find the Animator component in the
Inspector
tab. All animated objects have anAnimator
component added automatically. - Find the
Controller
slot in theAnimator
component inspector. - Drag and drop your
Animator Controller
asset into theController
slot of theAnimator
component. - Run the game to see your character play the default state animation of your Animator Controller. If the animation is not looped, it will be played just once and then the character will freeze.
- You can also select your character in runtime and navigate to the
Animator
tab to see what animation state the character is currently in. Current animation state will have a blue progress bar displayed.
Every animated object in Unity uses an Animator component and an Animator Controller asset. The component is responsible for playing animations in runtime. It has a number of parameters that we have to set or we can use to tweak the component's functionality:
Avatar
: In Unity,Avatars
are rig definitions. For instance, if we have multiple files containing animations with the sameGeneric
rig, we should use the sameAvatar
for all of them. You can find more information about it in the Using animations from multiple assets recipe.Apply Root Motion
: With this checkbox, we can turn the root motion on and off. It can be useful when we have animations with root motion but don't want to use the root motion definition for a given character.Update Mode
: This parameter tells Unity in which update the animations should be evaluated. TheNormal
option makes the animations synchronized with the normalUpdate()
call, theAnimate Physics
option synchronizes animations with the physicsFixedUpdate()
call, and theUnscaled Time
option synchronizes the animation with the normalUpdate()
call, but disables animation time scaling (the animation is played with 100 percent speed regardless of theTime.timeScale
variable value).Culling Mode
: This parameter tells Unity when to turn off the animation playback on a given Animator. TheAlways Animate
option makes the Animator always play animations (event when off-screen), theCull Update Transforms
option culls Retarget and IK Transforms when the Animator is not visible on screen, and theCull Completely
option disables the animation completely when the Animator is not visible on screen.
The Animator Controller asset stores a graph of animations (animation states) and defines the rules of switching between them, blending them, and so on. The controller (asset) is attached to the component's Controller
field (the component is attached to a character prefab or a character placed in the scene). Many objects or characters can share the same Animator Controller if they use the same animations (have the same rigs or are humanoid characters).