My journey becoming a Unity game developer: Enemy AI-Setup the guards to patrol the area (Part 2)
Objective: Placing waypoints around the area for the security guards path to patrol. Also, writing code in a modular way to allow each guard to move through the waypoints in their path.
Previously, we made the 1st Security Guard move from Point A to Point B. We have added a third waypoint name Point C for the 1st Security Guard’s path. Position Point B across from Point A waypoint behind the glass case, and create Point C waypoint at the 2nd guard’s position. This path will have the 1st Security Guard walk around the middle glass case near them to the 2nd glass case on their left.
Inside the GuardAI script in the Update() function, we can hard code the 1st Security Guard to move through each waypoint. Start by checking IF the current target is valid. If it’s valid, we want to use the Distance between the guard’s position and current target waypoint position.
If that distance is less than one unit, check if the next waypoint which is Point B is valid and the current target waypoint is not equal to Point B. If both conditions are met, the new current target will be Point B. Then, set the 1st Security Guard’s destination to Point B position.
Else, if Point C is valid we want to make the next target Point C. Then set the guard destination to the next target Point C position.
We see the 1st Security Guard moving to all 3 waypoints, and going back and forth repeatedly between Point B and Point C. The guard is moving back and forth between the 2 waypoints because Point B and Point C code is updating every frame, while Point A is only used once inside of the Start() function. Using code like this makes it difficult to add new waypoints for the guards path because we will have to hard code in every target waypoint for the guards path. This isn’t an efficient way to handle waypoints that might need to be added throughout the design of this game.
Back in the Update() function, we will create a more efficient way for the guard to move to each waypoint. Remove the previous code inside of Update(), then begin by checking IF the waypoints LIST is not empty, and the current waypoint target is valid. If so, set the security guard’s destination to the next target waypoint position.
Afterward, we want to get the Distance between the guard’s position and the current target waypoint position. IF the distance is less than one unit, increment up one the next target waypoint position.
Now to see if this code will work on all 3 security guards, we added extra waypoints to the scene, and positioned them around the room for each guard’s path on patrol. Make sure to place all new waypoints under the Waypoints game object to keep the Hierarchy organized.
Select each Security Guard and go into the GuardAI script component in the Inspector. Set the amount of waypoints each guard will use on their patrol, and assign each waypoint to each Security Guard.
Now the guards will move through each of their waypoints until they reach the last waypoint on their list. Notice that when they reach the final waypoint, they stop patrolling and will stay at that waypoint because they don’t know what to do next.
This is also the reason we’re receiving an error message about Index was out of range. Must be non-negative and less than the size of the collection. To fix this problem, we need to make the guards reverse their paths to keep them within range.