My journey becoming a Unity game developer: Player Movement-Activate the Walk animation through code

Rhett Haynes
4 min readOct 28, 2021

--

Objective: Make Darren go from the Idle animation to the Walk animation using the Animator and code to turn on and off the Walk trigger when Darren either reaches the hit point or not.

Walk animation turning on and off if he reached his destination or not.

In the Player script, create a new variable that will be a handle for the Animator and name it anim for short. Then inside the Start() function use the anim variable to get the Animator component which is under the Darren 3D game object. To gain access to the Animator, we have to use the GetComponentInChildren<> method. The Animator is under Darren 3D, Darren 3D is under the Player game object, therefore making the Player object the Parent of Darren 3D.

We can see here in the Hierarchy that the Player game object has the Darren 3D game object inside of it. Therefore, with the Animator attached to Darren 3D we must use the GetComponentInChildren<> function to access the Animator.

Player’s child game object is Darren 3D with the Animator component attached to it.

In the Update() function inside of the IF Raycast method, we will use the anim variable to set the Walk trigger to True.

When we set the Walk trigger in the Animator to True, Darren’s walk animation will play as he moves to his destination instead of him in the Idle animation.

Darren’s Walk animation playing when the Animator’s Set Trigger is true.

Now we’re receiving an error indicating that their is an Animation Event trigger activating to run a function attached to the Event which is missing a receiver. We’re not using the Animation Event at this time, so we can delete both events to get rid of this error message.

Animation Event function triggers deleted from the Walk animation clip.

Playing the game we see Darren moving in the beginning in the Idle animation, therefore he will be sliding across the floor to his destination. When we click the Walk trigger in the Animator ON, Darren will switch to his Walk animation as he is moving across the floor. This is not an efficient way for the user to play the game, therefore we will control Darren’s animations through code.

Darren moving in Idle animation with Walk trigger turn off, then with trigger turned on in the Animator.

Still in the Player script inside the Update() function, we will remove the anim SetBool line from the IF Raycast statement. We will replace it with a new IF statement that will check if the Player’s NavMeshAgent distance is near or at the destination point.

The NavMeshAgent will check the remaining distance from the destination to see if it’s near or at the destination by using the remainingDistance method to see if it’s less than the stoppingDistance method. If it is, we will use the anim.SetBool method to set the Walk trigger to FALSE which will have Darren in the Idle state. If Darren is far away from his destination, the anim.SetBool will set the Walk trigger to TRUE which will have Darren in the Walk animation.

Darren Walk animation is turned off when he reaches the destination point. If not, Walk is turned on.

Finally, we will see Darren in his Walk animation moving to the mouse clicks and stopping in his Idle animation when he makes it to his destination.

Animator shows Darren Walk animation turned on and off if he reached his destination or not.

Next time we will work on the Enemy AI for the security guards to stop Darren from achieving success in escaping.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet