Modular Waypoint System

A modular design or modularity is a way of designing different pieces individually that can then be used for the same functionality in different systems. Using this system it allows for each of the guards in the game to act independently. Each guard will have their own waypoints to follow. Using this design method, it allows for only a single script rather than using multiple for X amount of guards.
One way of thinking of modularity is Lego. Legos usually come in a “set” where it is “designed” to build a certain object, but at the same time it is possible to use any of those pieces from that “set” in a completely different one and come out with a desired result. Each piece is independent of each other, but because of that design it allows for different results in the case of Legos.

The Guard AI script starts out simple, and like a lot of other scripts. Declaring the variables and “items” needed for the Guard to do what it needs to do. A list of transforms for the waypoints, the NavMeshAgent as the movement system, a way to move through the waypoints, a reverse check and same with a way to check if we’ve reached the target. In start, you grab the NavMeshAgent and do a null check to make sure it doesn’t break.

Next is Update. This is where a good portion of our logic is being checked. A check to make sure we have waypoints in the List, if so then we can continue. We set the Agent destination, moving it, along to the start position. Afterwards, doing a distance check to check to see if we’ve reached the position we want to get to.

Lastly we start do our final checks in a Coroutine that we set up for the Guard to stop at the end points, as well as where we do our reverse system.

With this we wait a random amount of time between 2 and 5 seconds, if our position is at the beginning or end of the waypoints. If we’re reversing then the guard will go from X to 0, and if we’re moving forward it’ll go from 0 to X. X being the number of waypoints in the List.

Next I’ll be finishing up the other 2 guards and giving them all “eyes”. On to the next.