Using the Unity Animation System to Control Player Animation States

3D Animations will breathe life into your game and significantly help to pull your users into the fantasy of your game. Let’s talk about the Unity Animation System and how we can use it to control our player states from idle to walking and beyond.

First things first — we need a character model! Up until this point we have been prototyping our character with a capsule primitive, but the time has come to implement the good stuff. As part of the asset pack for this game we have a pre-rigged model for our main character, Darren. Before I update our capsule to our player model, a quick note on how to get animations for your game. There are a ton of pre-rigged assets that you can purchase or download for free online for use in your own projects and indie games. A favorite website of mine is which allows you not only to pick up character models, but also to pick up animation rigging that you can apply to any model. Do you have a humanoid character asset ready but he doesn’t have the behaviors you want? You can pick up walking, jumping, strafing, dancing, attacking and more animations than you would believe for free from It’s a great tool!

Now let’s get our character model in place. We take our existing capsule prototype and reset the scale to 1x1x1. Then we disable the mesh renderer so we are just left with our capsule collider and NavMeshAgent.

We now drag in our prefab “Darren” 3D model gameobject as a child of our Player container and modify the scale as needed to match the size of the other humanoid characters in our scene.

Back on the Player container, we need to adjust the size of our capsule collider to appropriately cover our model.

That looks pretty good. We also need to do the same with our NavMeshAgent:

Our model is now ready for action!

By childing the 3D model into the prototype Player object we used to calculate our movement behavior, we can easily swap the appearance of our asset in just a few steps while maintaining all of the behaviors.

Now to animate! Without a walking animation this just looks silly. For this game we have three animations to work from — idle, throw, and walk. Our default state (not moving) will be idle, and when the player gives us a new waypoint we will change the animation to walk until our player reaches its next destination.

Step 1 is to create an Animator Controller. In your project folder right click -> Create -> Animator Controller. This controller allows you to provide rules for when to switch between your animation states. Double click the new controller to open the Animator window. Next you want to drag in your default animation state from the project folder into the Animator window, in my case the “Idle” animation.

The transition arrow from Entry here indicates that as soon as the game runs we will begin this animation. Now we can apply the Animator controller by selecting our 3D model asset in the hierarchy, adding an Animator component, and dragging in our new animator controller into the designated field.

If we now play the game we can see our player is looping this default idle animation. Let’s add our other two throw and walk animations to the controller and we can begin setting up those behaviors.

Now we can create transitions between our states and set parameters for when those transitions should happen.

Simply right click and drag from one animation state to another to connect a transition. One important setting here to be wary of is the “Exit Time” in each transition. If this is enabled it forces the animation to finish its current loop before it can transition. In our case we want this to be off so that we start and stop our movement instantly.

Next, on the left hand side of the Animator window is a section for Parameters which is where we can create the triggers for our animation states for later reference in our code. In my case we want to create a bool for “isWalking” which will be true when our player is moving between waypoints and false if we are stationary.

Now we can click on the transitions between Idle and Walk and set the required condition for Idle to Walk to be that isWalking is true, and the opposite for Walk back to Idle.

In our player script we can now set this bool to true when we are walking to a new waypoint by getting a handle to our Animator component in the child object and then setting the bool to true in our raycast function.

This works great one way, we get our player walking but when he reaches the destination we do not yet have a means to return to idle:

One way to solve this problem is by calculating the distance from our player to our waypoint destination, and if that distance value gets small enough we can trigger the walk animation to stop. We do that using Vector3.Distance which returns a float value for us to compare against:

This looks like a good solution for our use case and it’s working properly in our game to return our player to his idle state:

That’s a wrap on player animation! I’ll get into the code required for throw later since this belongs to a coin toss mechanic we have not yet implemented which will serve as a guard distraction. For now, hopefully this is a useful resource for creating and controlling animation states in your game. I’ll get into setting up our guard AI pathing next. See ya there!

Unity Developer from Los Angeles, CA