Journal 69 — “The Great Fleece” in Unity, Guard Animations

Objective: To refine the Guard AI movement and add animations

In “The Great Fleece”, we just implemented the first guard AI movement. Now we want to refine the movement and add guard animations.

Guard Pause

Let’s add a pause to the guard movement. We can add a bool to check if the target is reached, and we can add a coroutine to pause for 2 seconds. We can also rearrange the code we created previously to increase and decrease the waypoint counts. Then you can see the pause in action.

Let’s refine the pause a bit to make it more random, and also let’s have the guard only stop at the beginning and end. To do this, we need to check in the coroutine when the waypoint currentTarget is either 0 or when currentTarget = wayPoint.Count-1 (i.e., for the first security guard with 3 waypoints, the currentTarget variable can only reach 2, since the first waypoint is at 0).

One other adjustment is to turn off Auto Braking in the NavmeshAgent, and add a nominal value to the stopping distance. This will smooth out the movement through the intermediate point so the guard doesn’t pause.

Now let’s see this in action.

Guard Animation

Similar to Darren, let’s create an Animator Controller for the security guards. We can also drag in two animations to the Animator Window, “Idle” and “WalkingAlerted” to start.

After bringing up the Guard Animator window, we can create a bool variable called “Walk”, make two transitions between Idle and WalkingAlerted, and turn off “Exit Time” and set the transitions to “Walk” = true and “Walk” = false.

Before seeing this in Playmode, we need to add an Animator component to the security guard and drag in the Guard Animator Controller. Let’s see the results. You can see the guard has a slight head movement and hand movement in the Idle animation.

Let’s fix the transitions so that the walk animation plays correctly and he idles when stopped at the waypoints. In the GuardAI C# script, we can add a variable to the animator component and get a reference in the Start Method. In the Update Method, we can check for if the distance to the waypoint is < 1, then the Walk animation should be false. Otherwise the Walk animation should be true.

As you see, there is a brief transition between Idle and Walk at the intermediate waypoint that looks odd. Let’s fix this.

We can add a check to make sure to only show the Idle animation at the beginning and end waypoints.

Finally, let’s see this sequence in action.

Thank you for your time!




An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Interaction: alert, prompt, confirm

Why Should You Upgrade to React 17

Free Code Camp: Data Visualization

Practical Godot — Project Setup

NestJS Project Starter with Multiple Database Connections using TypeORM and MySQL

Docker Learning Note (iv)

How to capture HTML and SVG into an image

Hey, leave those alone! -Application State and Function Purity

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chris Nielsen

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

More from Medium

Adding a ram attack to our boss

2.5D Platformer — Improving the Ledge Grab #10

IWTV is part of Indy Pro ‘22

Using Post Processing Volumes in Unity3D with the URP