Simple waypoint system in Unity

Niklas Bergstrand
3 min readMay 22, 2021

Thanks to the Navmesh Agent in Unity, it is fairly simple to set up a waypoint system. With the waypoint system you can set the pattern a guard will walk. Let’s go over how to implement this.

For this to work we will need a Navmesh baked into the scene. I demonstrate how to do that here.

Create a script with a suitable name. For this article I named the script GuardAI. Add the script onto the object that will use the waypoint system:

Open up the script. Add a public List of Transforms that will hold the waypoints and a private transform for the current target to move the agent to.
We also need a private NavMeshAgent and an Animator. Lastly, add three bools to check if agent is reversing, agent is at the last waypoint and also if agent is currently moving:

Initialise the NavMeshAgent and Animator in the Start function. Also set the agent’s first target to move to:

Create a coroutine called MoveToNextWayPoint. Further explanation is in the screenshot below:

In the Update function pass in the current speed percentage to the Animator. Check if agent has reached target position and currently moving, if so call the coroutine MoveToNextWayPoint:

The waypoints can be any 3D object with a Transform. To ensure that it has the correct proportions you can duplicate the guard object, strip away everything attached to it and then position it in the Scene view:

Once you have created all the waypoints and positioned them, you can add them to the GuardAI script on the inspector:

Final result:

Good luck!

--

--