Doing the rounds- A quick guide to creating a modular waypoint system in unity

In this quick guide I will cover how to make a modular waypoint system for your AI.

The first step we take is to create a new script for the AI. I called by GuardAI.

I have made a list of transforms which we will assign in the inspector so we can attach as many or as little way points as possible.

Our script is modular and will allow guards to have 0 or unlimited way points to move through. The guard will move between their waypoints then turn around, wait for a couple seconds then continue their on their route.

The first step in our script is to null check our references.

We can now check for assigned waypoints. If there are none assigned the guard can be assigned as stationary. We will add a variable which tells us how many way points we have by checking our waypoint list count and decrementing by 1 (this ensures we will not get an out of bounds error later in the script when we use this number to determine which waypoint transform to use from the list as our target)

Now what we will do is if the guard is not stationary then we can check the distance to our next way point. If we have arrived at our way point, we call a coroutine which will determine our next target.

If we arrive at either the first or last way point we want to change a variable which will determine if we are currently looping back or not then have our guard stop and turn around. To do this we call another coroutine which will rotate our guard and wait until this coroutine completes before continuing.

In our rotation routine we take our current rotation and add 180 to the y value to turn our guard around. We slerp between our starting rotation and our end rotation with a duration of 1 second checking or progress along the way. Once this is complete we will call a function to change our target and continue moving our guard.

Our function to assign the next way point basically checks if we are looping and then will either increment or decrement our target int variable (which we pass into the value variable in our waypoint s list).

The next step is to set up the transform objects. In the scene view I have created another empty game object called waypoints (to keep the hierarchy clean). It will be this object that will hold all of out way points.

Lets create our first way point. Create an empty game object. As my guards will not share or use each others way points I have named my way point Guard_1_ Waypoint_A and will continue using this naming system.

In order to ensure the starting point of my guard way point is correct I will drag the object into the guard object and “zero out’ the transform values.

We can then duplicate this object and place it at the end destination.

A quick tip here is to change the icon of the object and ensure you have 3d icons active in your gizmos in the scene view. This will allow you to see the where you have placed the waypoint. See below on how to do this.

The next step is to assign the new way points to our guards waypoints list in the inspector.

We can now test the script. I have speed this up in game for demonstration purposes.

That’s all for now.

Unity developer with a love of learning all things programming.