Adding walk and run animation in Unity

Niklas Bergstrand
3 min readMay 19, 2021

--

Before you run you need to learn how to walk. Let’s go over adding walking animation to our player. I will also explain how to add run animation via a feature called Blend Tree.

This article assumes that you have added point and click controls to your player. I go through how to add that here.

Start by adding an animator component on the graphics object of the player (the part that will be animated):

Create an Animator Controller and drag it onto the Animator component:

Double-click the Animator Controller to open up the Animator window:

Drag in the Idle animation first and then the Walk animation. The first animation that gets added will be the default state which is why Idle needed to be added first:

Add two transitions, one from Idle to Walk and then another from Walk to Idle:

Add a float parameter and call it speed:

Select the first transition and add speed and set the threshold to Greater than 0.1 and untick “Has Exit Time”:

Do the same for the second transition but set condition to less than 0.1:

Open up the Player script and add a private Animator and initialise it in the Start method. As the Animator is on the child object of the player, you will need to use GetComponentInChildren to reference it:

Lastly, in the Update function get the speed percentage from the NavMeshAgent by dividing the magnitude with the speed. Call the SetFloat function on the Animator and pass in the percentage value together with the name of the parameter:

If you have more than two a locomotion animations, for example Idle, Walk and Run it might be better to use a Blend Tree. Let’s go over how to add a Blend Tree instead.

The coding part is the same as the animation that I have just explained. Open up the Animator and add a Blend Tree. Double-click on the state to open it up:

Add three Motion Fields, and ensure the parameter is still set to speed:

Drag in animation for Idle, Walk and Run:

Good luck!

--

--

Niklas Bergstrand
Niklas Bergstrand

Responses (2)