Creating Modular Waypoint System in Unity

Next up, we have to start creating a movement pattern for our guards. To start, we will create a new script for them and attach it to all the guards, along with the Nav mesh agent:

From here, we are going to start with just 1 guard and have him move between the points. To create the waypoints, we will just duplicate the guards and delete everything attached to them so they can just be a place holder. Then, we are going to create a new empty object in the hierarchy so that we can store the waypoint locations there to keep our hierarchy clean:

Next, let’s get our first guard moving between the waypoints.
In our script we will need to access the AI library again, and follow some parts of the player script for our guards:

However, as we are making the guards run around on their own, we will need to create a “waypoint” walking system in which our guard will move from point a to point b, and so forth. As we will be having 1 guard just standing still, we need to have it work only if there are waypoints in place, so that we do not get errors when running the game. Finally, we will code in having our current target increase by 1 every time we get close to the next waypoint:

Now that we have a moving guard, we need to work on getting them to go back along their path. Otherwise, the game would be pretty easy if you just have to wait for them to reach their endpoint. To do so, we need to create a bool to work with, along with making a couple adjustments to our code:

What we are doing is telling unity that if it is close to the waypoint, to go towards the next one. That next one is determined by a couple factors.

  • Are we still moving along the path
  • Are we at then end of the path
  • Are we at the beginning of the path

As we will have it working in a modular system, we do not need to place any actual values in our script because you can have any number of waypoints active per guard at a time. Unity would know whether or not if it is going backwards or forwards.
Finally, let’s add in an idle state along with cleaning up our code a little bit. To start, we will want to create a coroutine for our guard movement to work off of, and from there transfer most of the code we just wrote into that routine:

What this allows us is to give the guards a quick pause between moving to the next waypoint, along with having our Void Update look a lot cleaner. The more we can clean up the code and group it into it’s own void, the better as it makes it easier to debug any issues that may arise, or add in something new if it is needed:

Now that we have a working idle time for the guard, let’s make a few quick changes so that the guard becomes idle when it reaches the ends of it’s path and not every waypoint it touches. To do so, we are just going to make a quick change to our Coroutine:

What we have done is told Unity that when we reach target 0 or the last target, which we have to set as Count-1, we want our guard to remain idle before moving along the path again:

Now that we have moving logic for our guards, we can look at creating a system in which they know when the player passes within their view.



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