Using Unitys Animation System Part 2 — Scripting

Yesterday, we set up Darrens walk animation, today we’ll have him actually walk the walk ‘in game’.

What we’ll need to do is get a handle of the Animator so we can use the .SetBool command on it. One slight challenge is the animator is in a child object. Fortunately, it’s easy to access with GetComponentInChildren, another option is using transform.GetChild(0). You’ve got options!

Next in our Update() right after we set our destination, we’ll tell the animator that ‘isWalking’ is true.

Let’s give it a try!

NICE! Except he doesn’t stop walking…That’s because we need to tell him to stop walking ourselves using the SetBool function again, but where? Logically, it would be when he reaches his destination. Let’s begin by creating a variable for the destination:

Next, we make destination equal to hitInfo.point. I refactored the first part of the code a bit because I thought it looked nicer and made more sense.

Finally, we check to see if the player is 2.1 units close to the destination and if they are, stop the walking animation:

And that’s all you need! Let’s see it in action!

Perfect! Next episode, we’ll talk about Modular waypoint systems!

cartoonist, game artist, and wanabe gamedev.