Managing animation states with the animator in Unity

As you start creating animations, you’ll accrue many clips. Some GameObjects are going to have many animations, so you’ll need a way to control which animations run when. It’s possible to do this through code, but why would you go to the hassle when Unity provides a tool that makes this easy?

Meet the Animator.

You can think of the animator as the director in a movie. It runs around giving orders of which animations to play when.

For instance, when a character stands idle, the animator could order an animation to move the head and portray breathing and fidgeting. When a character’s horizontal speed increases, the animator could order a walking animation; once speed surpasses a defined threshold, the animator could order a running animation.

With the animator, you define the conditions and their animation relationships. Then, in essence, the game takes care of itself.

Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.

Accessing the animator

To get started working with the animator, click Window ▸ Animation ▸ Animator to open the Animator window.

Note: When you first start working with Unity, you may confuse the Animation and Animator windows. Try a little memory trick: The anima-tion window is where you do crea-tion. The animat-or window is where you’re an organiz-er.

You’ll probably see an empty window because you didn’t select a GameObject that has an animator component attached to it.

This shows the empty animator window

In the Hierarchy, click BobbleArena and check if you see the following in the Animator window:

This now shows three animation states in the Animator window. There is an Entry, Walls, and Any State state.

You’ll see that the Walls animation is already present in the controller. Any new animation clips are added to the current animator controller automatically.

Each rectangle indicates an animation state:

  • The green rectangle is the entry point of the animator.
  • The orange rectangle indicates the default state; it’s always played first.
  • The arrows indicate transitions between states.

By scrolling your middle mouse wheel up, you can zoom in on the states. Likewise, scrolling down zooms out. Pressing and holding the middle mouse wheel allows you to pan over the states. These shortcuts are useful when you have a lot of states onscreen at once.

Play your game and move the hero until you can see the lowered walls. The animation loops endlessly.

This shows the space marine fleeing the aliens

Watch the animator as it plays to see the animation’s progress.

This shows a transition from the Entry to the Walls state

Configuring an animation state

In the Project window, click the Animations folder and select the Walls animation. In the Inspector, uncheck Loop Time.

This shows animation properties

That’s how you override the default — no loop for you!

Unfortunately, “Walls” isn’t a very descriptive name. Click the Walls state in the Animator and look at the Inspector to see all the available properties. You’ll learn and change a bunch of these throughout this book.

For now, change the name from Walls to WallsLower. That’s it in here.

This shows the updates state name to WallsLower

Notice that the state reflects the updated name.

This shows the state with a new name.

Being that you have a WallsLower state, you need a WallsRaise state. You could create a new animation, but since this animation is very similar to the WallsLower animation (just reversed), I’ll show you a shortcut.

Creating an animation state

Right-click in the Animator, select Create State and then Empty.

The shows the dialog for creating a new empty state

You’ll now have a new empty state.

This shows a fresh new state

Select this new state and name it WallsRaise in the Inspector. Next, drag the Walls animation from the Animations folder to the Motion property. Finally, set the speed to -1.

This shows the Walls Raise name with the updated motion and speed.

The Motion property references the actual animation clip. In this case, you’re using the same animation clip that you used in WallsLower. The Speed property lets you designate how fast the clip will play. Setting the speed to 1 indicates that it will play at normal speed. Higher numbers make the clip run slower, whereas lower numbers make it run faster.

Why -1? Negative numbers play the clip in reverse. In this case, you’re using the same clip but playing it in reverse at normal speed.

You need one more state: An idle state. Open the Animation window, making sure the BobbleArena is selected in the Hierarchy. You’ll see the name of the currently displayed animation in the property list, which should be walls.

Click Walls and select Create New Clip… from the drop-down.

This shows creating a new clip

Give it the name ArenaIdle.anim. That’s right — your animation clip comprises an empty animation. Although you can create a state with no associated animation, Unity will do weird things that might ruin your plans. At times, it helps to play it safe and make an animation clip for the idle state, even if it’s empty.

Your animator should look something like this:

This shows the new states.

You need to organize it, and you’ll do that through transitions.

Where to go from here

Animators and animations can be perplexing at first due to their names. Thankfully, the confusion period is brief. You’ll be able to tell the difference in no time.

At this moment, there is no way for you to move between the various animation states. Thankfully, Unity has you covered by way of animation transitions. You’ll put these to work in the next tutorial.


Discover more from Jezner Blog

Subscribe to get the latest posts sent to your email.

By Brian Moakley

Brian Moakley is a writer and editor who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply