Make Guard Stop When Reaches a Waypoint

Creating guard idle state

Rusben Guzman
2 min readNov 16, 2021

Objective: check when the guard reaches the first and the last waypoint and stop him for a few seconds.

When I created the guard waypoint system, the functionality made a continuous movement. Now I want him to stop in the first and the last waypoint.

In the GuardAI script I declare a bool variable called targetReached. The idea is to use this variable as a flag to execute or not the guard movement.

Next I created a coroutine called WaitBeforeMove. This coroutine sets the value of targetReached to true, waits 5 seconds and sets the value to false.

In the update function I check if the targetReached is equal to false in the first if statement. If targetReached is true it means that the guard is in the waypoint and he has to stop.

Then I added another if statement to check if the currentTarget is the first or the last waypoint to call the coroutine. With these 2 conditions the guard is ready to stop when reaching the first or the last waypoint.

And that’s it!!! Now the guard makes a stop before continuing on its way.

--

--

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.