Creating modular waypoint system in Unity
In the Stealth 3D game, the security guards will be AI agents, so they must move towards “waypoints”.
The first thing is to create waypoints. One way to do it is:
- Create an empty object. Reset it. Rename it as
- Select a guard. Duplicate it.
- Move the last one to the desired position. Copy component values
- Create an empty gameobject in
Waypoints. Paste component values
- Repeat over and over as much as needed
- Create a new script called
- Create a list of type
- Declare a new variable
Start, check if null and set
- Declare a
NavMeshAgent. Set its destination
public List<Transform> waypoints = new List<Transform>();
public Transform currentTarget;
private NavMeshAgent guardAgent;
private void Start()
if (waypoints.Count > 0)
if (waypoints != null)
currentTarget = waypoints;
- Attach this script to all the AI guards
- Place each waypoint in the guards’ lists
We will need to add some functionalities:
- Set the
- Increase and decrease its value with a boolean variable
- Set the guard to wait for some seconds after the waypoint has been reached using a
After following the video about coding the mechanic of making the guards to stop for a while at the first and last waypoints, I decided to write the code on my own,with my own style, and after some hours, I did it!
It feels really good when someone can make things happen the way they want!