Change player animations using Unity’s Navmesh

Daniel Kirwan
3 min readJun 22, 2021

--

Objective: Change the player animations from walk to idle once they have reached the destination

Now that our player can move along the navmesh using the point and click system I created in a previous article it is now time to animate the player so that it looks like they are walking.

First thing to do is to create an animator controller in the project window.

This will be the controller that I will add the animations to. Add an animator to the player and drag in the newly created animator controller. Then double click the controller to open up the animator window.

Now that the animator window is open drag is the animation that you would like to be the default. It will appear orange in the controller.

You can change this at any time but dragging in the default state first is a small time saver.

Next I dragged in the other animations that I will eventually use for the player. I have connected the idle animation to the Walk animation using transitions.

Before adding in the transitions, I created a parameter to allow me to activate the animation changes. This is a bool called Walk.

Now right-click the idle animation and create a transition to the walk animation. Repeat this for the opposite way.

Now the set up of the transitions, click the white lines with arrows. First select the idle to walk transition and deselect has exit time. Now click the + button in the conditions section, it should auto select the parameter already made with true. Now select the other transition and do the same, but change the true to false.

Now is the code that will make it happen. The difficult part to this is determining when the player has arrived at their destination and changing the animation to idle.

Changing to the walking animation is easy as it can be done when the player clicks the left mouse button.

I have set up a bool for when the animation is walking and added in a reference to the animator on the player.

The next part is the tricky part. I need a float that will be the distance to the target when the player clicks the mouse button. I have also set up a destinationThreshold. I now check to see if the distance left to travel is less than the threshold. I then change the animation to false and set the bool to false as well.

In the update method where I am moving the player I have a bool check for determining if the walking bool is true, if it is true I then run the above method.

And that is it for this article on changing animations using Unity’s navmesh.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet