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”.
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:
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:
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.
Finally, all that work pays off with a very nice looking waypoint system!
In Part 2, my next post, I will add the possibility for alternate paths, and add functionality for turning slowly to face the LookDirection.