My journey becoming a Unity game developer: Enemy AI-Setup the guards to patrol the area (Part 3)
Objective: Make the security guards patrol the area moving forward and in reverse along their patrol path. Also, have the guards pause between 2 to 5 seconds at each waypoint using a coroutine method.
Now our Security Guards are moving from the 1st waypoint to the last, but the guards are still trying to move on to the next waypoint except that they’re at the end of the list with no more waypoints. In the GuardAI script, since the current target waypoint is incrementing up one every frame in Update(), the Security Guard is being told to find the next current target waypoint indefinitely.
Let’s make the guards reverse their path when they reach the end of their list. First, we will create a new Bool variable name reverse set to false to check if the guard is moving in reverse or not. IF the distance between the guard and waypoint is less than one unit, we want to check IF the guard is moving in reverse. If True, have the current target waypoint decrement by one.
Else, we will make the current target waypoint increment by one. Also, check IF the current target waypoint equals the last waypoint in the wayPoints LIST using the Count method. If it does, set the reverse variable to True, and decrement the current target waypoint by one. We need to do this to prevent the current target waypoint from incrementing to infinity.
As we see the Security Guards moving through the waypoints, we will notice the guards moving forward and in reverse through their paths. But, now when the return to the 1st waypoint in their paths, they stop on the waypoint and will not move anymore. In reality they’re actually trying to move to another waypoint that is not on the wayPoints List because they’re looking for negative element numbered waypoints that doesn’t exist.
When the current target waypoint decrement it will be decreasing to infinity. Therefore, we need to use another IF condition to check if the current target waypoint equals zero. If it is, set the reverse variable to False, and set the current target waypoint to zero which will prevent the current target waypoint from decrementing into negative numbers.
Now the guards are moving through the waypoints forward and in reverse continuously patrolling the area.
One final thing we want to do is have the Security Guards pause at every waypoint before moving on to the next one. The best way we can do this is to use a Coroutine method.
Let’s create a new Bool variable name targetReached which will notify the guard that it reached its destination. Back in Update(), we want to check IF the distance is less than 1 unit AND the guard didn’t reach its target. If the conditions are met, we will set the targetReached variable to True. Then we will use the new coroutine created name GuardDelay. To use a coroutine we need to use the System.Collections namespace to access the library of classes and methods used for coroutines.
Create the GuardDelay() coroutine using the IEnumerator as Type. First thing we want to do is make the guards pause when they reach their target waypoints. We will use the WaitForSeconds, and give them a Random Range between 2 to 5 seconds.
Then we will take the code checking IF we’re in reverse or not in Update(), and move it into the GuardDelay() coroutine behind the WaitForSeconds() line. This will be the next step after making the guards wait a couple of seconds before moving on their patrols. Last, set the targetReached variable to False again which will allow the guards to go to the next target waypoint. If we don’t set targetReached to False, they will remain stuck at the current waypoint target they reached indefinitely because of targetReached set to True in the Update() function.
The guards will now go to each waypoint and wait between 2 to 5 seconds before moving on to the next waypoint in their paths.
Next time we will have the guards pause only at the first and last waypoints, but not at the others in between.