Using the Unity Animation System

Objective: Setting up the walking and Idle animation for our player.

Rose Owen
6 min readJul 14, 2021

So we have set up the NavMeshAgent and got our player moving around now what? Time for a propper player model with animations based on if they are moving or staying still sounds like a good starting point.

This might be a bit easier for me since I already have the model and the animations ready. Although, utilizing free resources like Mixamo you can get all your animations done quickly and easily.

The Animator:

Here we can add in the animations for the player, if you don’t already have it open you can go to Windows>Animations>Animator, after opening it we can dock it somewhere useful.

After clicking on the model for the player, we can select the animator window to show the animations connect to that player. If nothing is showing up, this might mean you don’t have an animator or controller on the player. Click Add Component, search for Animator, and Click on it to add one.

Next, make a Controller for the animator. Right-click in the Project tab and then Create>Animation Controller. Give it a name that fits with the player, In my case, the player is named Darren so I named the Controller simply Darren, so I knew it was for Darren.

Now we can head back to the player model and assign the Controller for the animator. Either drag the controller into the field for the controller or click the circle at the end then select the controller.

Now when we go back to the animator tab it should show us something like this.

From here we can drag in our animations for the player, to take 1 less step, drag the idle animation in first as it automatically sets the first animation you drag in as default. This can be changed at any time so it’s not super important. We have 3 animations for this game so far, Idle, Walking, and Throw.

If you dragged in a different one first and Idle isn’t orange like this, right-click on the one you want as the default state and click the Set as Layer Default State option. Now when the game starts up, this animation will play.

Now we have the player animating when the game is playing but when we try to move it stays in that idle animation, to change this we will need to add in transitions through code and a little bit more in the animator. Let’s start with the animator first.

Add a bool parameter by clicking the + and then selecting Bool, Give it a name like walking.

This will be used to say if we are walking or not.

Now we can set up the transitions, right-click and select Make Transition, then click the state we want to transition to, in this case, the walking state.

You should see a line connecting them with an arrow pointing towards the walking. Since we want to be able to change back to Idle when we stop walking, make another transition between walking and Idle.

Click on the line with the arrow heading towards the walk state. This will give us some options as to when and how the animation is played. Much of this we will skip over since we don’t need them for this.

“Has Exit Time” We will want to uncheck this, this is used when you want an animation to finish before moving onto the next animation, in our case we want to change to the walking as soon as we start moving.

“Conditions” This is where we can set conditions to be able to change to this animation. The perfect place to add in our bool we made just before. Click the + and it’s done. Since it will automatically add the first parameter and have it as is true, which is what we want.

1 done, 1 to go. Change over to the transition from the walking to Idle and do the same as we just did before, this time changing the true on the conditions to false.

At this stage nothing in terms of the game has changed, the rest we need to set up in code. Open up the player script here we will need to make a reference to the animator and since my animator is on the model of the player which is a child of the player object, I used GetComponentInChildren. If it was on the same object, you would use the normal GetComponent.

Remember to Null check.

Next, head down to the movement we made in the last article, which can be found here. Here we can add in a line to set the bool for the animation to start playing when we click for the player to move.

Make sure the name you used is exactly the same as the Bool we made, it will not work otherwise.

And now we have an animating player when they walk. Just 1 downside…

Yup, when he stops the animation keeps playing, time to fix that. Here we can calculate the distance from the player to where we clicked and if it’s closer than a distance, change back to Idle. Although, since the hit.point is contained in the if statement for the ray cast, we won’t be able to use that and will have to make a new variable to store that.

Now we can assign the hit.point to it so we can use it outside this if statement.

While still in the update method, check the distance from the player to the destination using a Vector3.Distance.

and lastly, if the distance is less than 1 then change animation back to idle/bool to false.

And with that, we have a working animation to start and stop movement.

Another part of this stealth game has been finished, what will the next one be about? I guess you will just have to stick around to find out! See you in the next one!

--

--

Rose Owen
Rose Owen

Written by Rose Owen

Game Developer in the making~

No responses yet