In Part 1
we created a simple Nav Mesh, and made an agent walk on it, and in Part 2
, we added some obstacles to introduce some actual need for navigation. In this part, we will link multiple meshes together and use off-mesh links
to join them. This will vastly increase the capabilities of our navigation.
Make sure you have the project completed up to the end of Part 2
before continuing with this part. If you want, you can download the project as it should be at the start of this part from the bottom of the tutorial for Part 2
Off-mesh links allow your agent to navigate outside the Nav Mesh. For example, you might have a puddle in the middle of your terrain, and create a link that will allow the agent to jump over the puddle to the other side.
Some other uses for off-mesh links are:
- jumping to a position not directly connected on the mesh (e.g. across a gap)
- moving between multiple meshes
- shortcuts to positions on the mesh that would otherwise require a lot of walking (e.g. jumping over a wall instead of going around it)
- a door that must be opened before travelling through it.
- Create a duplicate of your ground object, and move it somewhere where it does not touch the original ground (make sure there is a gap between the two ground objects).
- Go to the Navigation window.
- Select the ground duplicate in the Hierarchy.
- Select the Object tab in the Navigation window.
- Make the ground duplicate walkable, navigation static, and tick the Generate Off-Mesh Links checkbox.
- In the Bake tab, set Jump Distance to 2 (under the Generated Off-Mesh Links area). This determines how far the agent can jump off-mesh, and you need to ensure this value is big enough to cover the distance between your two floors, so adjust this value if you have problems.
- Off-mesh links are by default one-way, so we need to enable the links from the original ground object as well, so repeat steps 4-6 for the original ground piece.
- Re-bake the Nav Mesh.
You should now see something similar to the image below in the Scene view, with the black lines representing the paths between the two meshes, and the black circles representing the start and end points of each link.
Now try running the scene and click on the second piece of ground and watch your agent skip over to the other part of the mesh. In a game you would add some animation to show the player jumping (or whatever fits your game).
Experiment with this. Try making a few different sections and linking them.
Manual Off-Mesh Links
So far we have just used automatic off-mesh links. These are great for the basics like linking two regions of walkable terrain together, but if you want to do more interesting things with your terrain, you will want to add some manual off-mesh links.
An off-mesh link is a regular Unity component you can add to any GameObject. Let’s use the Off-Mesh Link component to make a platform our character can jump up to and down from.
Start by adding a platform in the middle of your Nav Mesh somewhere. Just create a cube and stretch it to make a platform. Something like the image below should do. Also set it as a walkable surface, then re-bake your Nav Mesh.
Although this surface is walkable, your agent cannot yet get to it because it is too high, and separated from the rest of the Nav Mesh. So we need to add an off-mesh link to connect the platform with the rest of the Nav Mesh.
- Select your platform object in the Hierarchy.
- Add an Off Mesh Link component to it. You will see that the Off Mesh Link component has a few settings. The one’s we are interested in for now are:
- Start (the start position of the link)
- End (the end position of the link)
- Bi Directional (whether the link goes both ways is is one-way only).
An Off Mesh Link requires two Transforms to set its start and end points. These are the points the agent travels to/from when making use of the link, so one end goes on one Nav Mesh piece, and the other goes on the part it’s linking to. The transforms need to be placed on the Nav Mesh at appropriate places for the link to work.
- Add two new child GameObjects to the platform object and call them Start and End.
- Drag the Start and End objects into the corresponding fields of the Off Mesh Link component, like this:
- Position the Start and End objects so that one is on the ground of the main mesh, and the other is on the platform. Use this image as a guide:
It may be helpful to attach something visible to the Start and End transforms to make it easier to see.
Run the scene. Now your agent should navigate up onto the platform by ‘jumping’ across the Off Mesh Link.
If you want to experiment more, try adding some more Off Mesh Links or even more platforms.
Here is a download of the project as it should be at the end of Part 3