Waypoint-Patrolling AI With Branching Paths In Unity, Part 1

For my stealth project, I had to make patrolling guards. However, I decided to mix it up with some randomly-selected alternate paths, and a “look direction”.

Vincent Taylor
Nerd For Tech
3 min readOct 25, 2021

--

Today’s Objective: Create a simple modular AI guard that will patrol along some waypoints, and sometimes choose 1 of 2 waypoints to go to.

Creating the system:

The Variables:

I create a new class called “AIWaypointPatroller” which will hold all the AI code for patrolling along a set of waypoints.

I want to allow for facing a specified direction while at each Waypoint, plus the option for alternate Waypoint positions (which I’ll add in Part 2), so I create a custom struct for the List<> instead of just a Vector3.

I also want to show the Waypoints in the Scene view for easy debugging.

Finally, the private variables:

The Functions:

Awake:

Update:

Functions called during Update:

Finally, to draw the nice debug lines in the Scene View, make an OnDrawGizmos() method:

There are 3 parts I want to draw:

  • The current position,
  • The Waypoints and the lines between them,
  • And the Look Direction of each Waypoint.

The Result:

Finally, all that work pays off with a very nice looking waypoint system!

A waypoint path

In Part 2, my next post, I will add the possibility for alternate paths, and add functionality for turning slowly to face the LookDirection.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Vincent Taylor
Vincent Taylor

Written by Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.