Unity player animation

Hal Brooks
3 min readAug 21, 2021

--

Objective: Animate player between idle and moving with NavMesh

Add prefab Darren_3D to the Player capsule with scale (X 1, Y 1, Z 1), and uncheck Mesh Renderer to hide capsule. Adjust the Capsule Collider to fit the model for Darren using the edit collider button then adjust the base offset, radius and height of the Nav Mesh Agent, see below.

Visual of adding Darren, hiding capsule and final Nav Mesh Agent and Capsule Collider settings

Within the project window create an animator controller named Darren_Controller, placing it in the Animations folder. Add an Animator component to Darren_3D and assign the newly created controller as the controller for the animator component. Double click on the controller to open the animator window.

Next drag the idle and walk animations for Darren into the Animator window, shown below. Make sure the Walk animation has exit time is unchecked. Create a transition for idle to walk and walk to idle. Click + menu and add a bool parameter, naming it Walk.

Darren Controller animator setup.

Double click on idle to walk transition and under conditions select Walk and true, as shown below. When the Walk bool is true the walk animation will play. Similarly set the condition for walk to idle animation to Walk and false.

Idle to Walk transition.

In the Player script create a private Animator _animator variable and assign it in void Start, see code below.

Assigning _animator variable in void Start()

The Nav Mesh Agent has a bool, hasPath, that can be used to detect whether the player is moving to a destination. The Nav Mesh Agent, previously assigned to _agent variable, is checked for hasPath true within the void Update() of the Player script. Walk animation is activated while moving to a destination; otherwise, plays idle animation.

Code in void Update() to switch between idle and walking animation.

As seen below, the player now walks to new destination and is idle upon reaching that destination.

Darren with idle and walk animation enabled.

This article uses a Unity Store asset, The Great Fleece from GameDevJon.

--

--

No responses yet