Welcome to part 5 of my Pathfinding in Unity tutorial series. In part 3 and 4 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. These were pretty straight forward, bordering on crude implementations, but enough for you to get something up and running.
Now each of these examples, was reliant on a very specific constraint I set up in this series from the beginning. My navigation mesh is being baked onto one large plane and I’m carving chunks out of the plane using the nav mesh obstacle components. Now the thing is, that’s not how environments are built. Game worlds are not one massive chunk of terrain, they’re large number of individual pieces of terrain that are snapped together to give the illusion it’s one area. So with that in mind, how does a navigation mesh work across these small individual pieces? We can build a nav mesh on each of these individual pieces of geometry, but what about being able to navigatel across multiple pieces of geometry to get to a destination? Perhaps even more interesting, what happens when I start leave gaps between meshes. Can an agent navigate across that space?
In this tutorial I’m going to show you how Unity enables these features. Including an example of a special component used within the nav mesh system known as Off Mesh Links.
Snapping Geometry Together
First of all, how do we deal with multiple pieces of geometry? Well the good part is that Unity largely deals with that for you provided you run the bake settings correctly.
So in the example above, I have a plane set up in the world. In addition, I have used the simple NPC that I built previously as well as the simple destination location. If you’re not sure how these work, jump back to part 1 where I make each of these from scratch. Now I have set the destination for the NPC to be the transform of the sphere. In addition, I also baked in the navigation mesh on the Floor gameobject. So nothing we haven’t seen before, except that the destination location is not on the navigation mesh. It’s actually over the edge of it. So what happens when I let this run? The NPC will walk to the edge of the navigation mesh setup on the floor plane, but it won’t go any further than that. It’s also smart enough to not walk over the edge and fall to it’s doom. Which is actually kind of a neat trick. So how about if we just add the another piece of geometry that fits up nicely to it?
Now if you want to snap another piece of geometry to it such that it is flush. You can either sit and play around with the values in the inspector to get it to line up, or alternatively, grab a corner of it and hold the V-key which will allow you to snap it against the other piece.
Now if we run it again, it still doesn’t work. Why? Well the navigation mesh hasn’t been baked for the second part. Now when I go back in and bake that second one, you’ll notice that the two nav meshes link together. The nav mesh system is pretty clever and figures out that these two planes are next to each other, with navigation meshes being baked on them, so it ensures that the two link together. Meaning that this time when I play it, the NPC successfully walks over without any problems.
You can generate the navigation mesh in one batch in different ways:
- Select all of the items you want to be generating the navmesh for and bake.
- Alternatively, make them all child objects of another game object, then bake the parent and it’ll bake both of them at the same time.
Now, there are still points when this does not work: namely when I have a setup where the terrain has a gap between each piece. This is when we use Off Mesh Links
Off Mesh Links
Off Mesh Links are special link ups that allow for an agent to cross from one navigation mesh to another. This is ideal if you want to have an agent that can jump between navigable areas or can fall down from higher to lower points.Now there are multiple ways in which you can place off mesh links into a world, as well as a variety of configurations. Given you might not want a NPC to be able to climb back up from a drop that they’ve previously used. In addition, we can have them placed manually or automatically. In this tutorial I’m going to focus just on the automatic construction that can be done by the nav mesh generation.
So to resolve the issue of two distinct pieces of terrain that are apart, I can tell the navigation mesh to create off mesh links between the two planes. In the Bake tab of the Navigation pane, we can change the Jump Distance to enable this. In addition, I need to make sure the GenerateOffMeshLinks button is activated in the Object tab.
Then we bake it and we can see that a collection of off mesh links have been placed that link these two locations together.
When we press play, you’ll note that the character now ‘jumps’ across. With the behaviour looking a little different. Note that the character doesn’t ‘jump’ realistically, rather they simply move from one link to another. This is pretty much expected, given it’s providing us the functionality, but not the visual behaviour. Having it actually jump would be more of a requirement for an animation system to give us the visual behaviour we want in this instance.
So with this tutorial complete, we now have means to generate larger nav meshes across multiple surfaces and even link distinct nav meshes together if they’re slightly apart from one another. This allows us to build more interesting navigation spaces for both players and NPCs. In future tutorials, I’m going to continue this process and show more features of off-mesh links and how we can utilise them to create unidirectional paths (i.e. paths that only go in one direction), how to place them manually, how to create vertical gaps and a bunch of other small tweaks you can make to get the most out of your NPCs.