Introduction to Unity’s Animator

Learn how to control your animations in Unity

Pablo Gómez Platón
Geek Culture

--

The Animator is Unity’s main visual tool to handle multiple animations in a GameObject based on a State Machine approach (only one state can be active per layer, states can transition between them if certain conditions are met).

In this article, I will only focus on the basic usage of this tool, showing you how the Animator State machine works, how to create new parameters, how to create transitions, and how to modify the existing parameters through code.

How to start

Head to Window > Animation > Animator to open up the Animator Window.

As you can see in the image, it’s completely empty. That’s because it needs an Animator Controller to work with. You can create one in your project window by right clicking anywhere. Once you have created one, drag it to the GameObject you want to animate. An Animator component will be added to your GameObject, and now you can start adding animations to it.

In my case, I will drag it to my protagonist 3D model.

Understanding the Animator

This section deals with a somewhat advanced topic. It will help you to quickly grasp the concept of State Machines and properly understand the core of the Animator, perhaps helping you in the future with certain features for your project or game. You can skip this section if you just want to start using it.

As I’ve said earlier, the Animator is a tool based on a State Machine. It’s a system which holds a group of “states” (or decoupled behaviours) where only one state is updated or maintained active, and between states exists multiple sets of instructions that define ways to transition from an active state to a different inactive one.

However, the Animator has its own differences from a standard State Machine, and those are:

  • States are animation clips. There are some animation settings you can modify, but they do not hold any code functionality (there’s a way to add functionality, but that’s out of scope for this article).
  • The Animator has Layers, meaning that inside an Animator Controller (or State Machine) there are multiple instances of different machines running. This is very useful if you need to override certain animations (for example, a character who can point with one arm towards one direction while still retaining a walking cycle animation).
  • Every layer has an “Any State” event. Sometimes, some animations will need to be played, no matter the current state of the machine. This event, given the condition(s), will transition to other states. (A jumping animation is usually handled through this event).
  • Transitions have more settings than just conditions. Because the Animator deals with animations, there are some values that can be modified to properly adjust animation transitions. (More on Transitions in the next section).

Using the Animator

Drag or create your own animations into the Animator. I’ll be dragging two clips: Idle and Walking.

One of the animations will be set as the default animation that will be the first to start playing. If you want to change the default animation, right click on Entry, Set StateMachine Default State, and click on the desired default animation.

Now that the animations are inside the Animator, I’ll need to create transitions between them. To create one, I right click on the animation and select Make Transition.

Now I have to change a couple of settings of both transitions to avoid two issues:

  • There’s a “Has Exit Time” setting that won’t allow a transition until the animation reaches a completion percentage. I don’t want to wait for almost the entire duration of the Idle or Walk animation to allow the transition to happen. Unticking that setting on both transitions will make the animations more reactive.
  • There are no conditions, meaning that it will constantly be transitioning from one state to another. To fix that, I’ve created a new float Parameter called “ControllerSpeed”, and set the conditions to be higher or lower than a certain value.
Idle To Walk: ControllerSpeed > 0.1 | Walk To Idle: ControllerSpeed < 0.1

And finally to get everything working, create a new script, reference the Animator component, declare a new string to write the Parameter name, and Update that value through code.

Result

Behind the scene

--

--