Welcome to part 4 of my pathfinding in Unity tutorial series. In part 3 of this series, I looked at setting up a simple NPC that patrolled a set of waypoints courtesy of a navigation mesh built within a scene.
It’s pretty straight forward, but it suffers from a pretty nasty design flaw, in that it currently requires me to hardcode the list of patrol points into the patrolling agents code via the inspector. It would be a touch more convenient if I simply gave it a random destination and then based upon where it ends up it can figure out where to go next. So in this video I’m going to rebuild the example in part 3 such that the waypoints themselves know what locations it can visit within a certain radius and then it can randomly select from them.
Building an Adjacency List
So first off, let’s start with a new implementation of the Waypoint class from part 3. The original was pretty straightforward, but I’ve made some changes as you can see right here:
Let’s walk through the changes I have made. The first is that the ConnectedWaypoint now maintains a list of other ConnectedWaypoint instances, with the intention of dynamically selecting a nearby location rather than that being strictly managed by the order of the list. This is something that we will see in the new patrol class, in that it is no longer responsible for selecting the next waypoint in the area to visit. We allow for the waypoints to dictate this based on what is knows is connected to it.
The Start() method is now used to gather all ConnectedWaypoint instances that are within range of _connectivityRadius. This is then stroed in the _connections list such that we can use them later. Meanwhile the OnDrawGizmos() method is now drawing the debug draw radius as well as the connectivity radius in red and yellow respectively. Finally, NextWaypoint() takes in as a parameter another of the ConnectedWaypoint instances (we’ll see it being applied shortly) with the intent of finding another waypoint to visit that isn’t one that we have recently visited – which is the parameter.
Rewriting the Patrol Agent
With this complete, we now go ahead with re-writing the patrol agent. Now this time the patrol agent is shifting the destination selection out of the patrol and into the waypoint.
The biggest changes are in the Start() method and in SetDestination(). Firstly, Start() runs checks on whether we can find a waypoint that is a starting point. It gathers up all waypoints in the scene and simply selects one at random. Meanwhile, SetDestination() now seeks to find the next waypoint by asking the current waypoint for another location (which is the NextWaypoint() method we built in the ConnectedWaypoint class. In essence asking: ‘tell me where to go next, but without sending back to where I’ve already been’.
So with this fourth tutorial complete, we now have a simple navmesh patrol agent. It’s nothing fancy, but it shows that just using the most basic of the navmesh tools allows for me to create an agent that can move around the space and do something interesting. Starting in part 5, we’re going to look at how to use more of the tools built within the navmesh system. We’ll be starting with off mesh links: a means by which to have two different navmeshes for two different surfaces connect to one another. This is a really useful tool, given that navmeshes are typically going to be built from surfaces of numerous game objects in the same scene, rather than just one plane like I have done so far. So we need to know how to address that.