Many times in Unity, you will want to
connect two nav meshes together to allow
characters to move between them. For
example, you may want to allow a
character to jump over a gap in the
ground or drop down from an elevated
height. We can achieve this effect using
off mesh links. Off mesh links are
pathways that connect pieces of a nav
mesh so that naves mesh agents can
traverse them. For more information on
nav mesh agents, see the lesson linked
below.
We can create off mesh links in two
ways. We can either allow Unity to
generate them automatically or we can
manually place them with the off mesh
link component.
Here we have a scene that has a capsule
with a nav mesh agent attached.
The capsule also has a script that will
make the agent move towards a target
game object.
As you can see, the target game object
is on the other side of a gap and is
thus on a different piece of nav mesh.
In order to get Unity to generate off
mesh links for us, we will need to
select all navigationally static objects
in our scene that we want links between.
In this case, we will want to select the
two storage boxes. Once selected, we
will want to check the off mesh link
generation checkbox.
Next, we will want to go into the bake
tab of the navigation view. The two
properties that affect off mesh link
generation are drop height and jump
distance.
Drop height dictates how far of a drop
is an acceptable distance to create a
link. For example, it would be
reasonable to be able to drop off of a
box, but not a skyscraper.
The jump distance determines how far of
a gap an off mesh link can span. Once
again, the bigger the number, the
further an agent can jump from one nav
mesh to another. By default, these are
set to zero. So, we will want to set
them to something that makes sense for
our scene.
It is worth noting that in order to have
Unity generate off mesh links, you will
need to disable the height mesh
property.
When you're done setting your
properties, you will simply click the
bake button. You will see any generated
off mesh links in the scene view.
If we run our scene, we can see that the
agent is now able to traverse the gap
and reach its destination.
The second way to create off mesh links
is manually with the off mesh link
component. Creating links this way gives
us better control over where they are
placed and how we can interact with
them.
Here we have the same scene as before
except we have removed the generated off
mesh links and added two new empty game
objects which are named link start and
link end.
These will be the game objects that we
create our link between.
To add an off mesh link component to an
object, click the add component button
and select navigation off mesh link.
The first two properties of the off mesh
link component start and end require the
game objects that will represent the
start and end of our link. This means
that the component doesn't have to be on
an object that is a part of the link.
Though organizationally it is a good
idea.
The cost override is used to adjust the
cost of the link for the purposes of
calculating a path. If this property is
not a positive number, it will be
ignored and the default value will be
used.
Birectional dictates whether or not a
character can traverse in both
directions across a link.
The activated property allows you to
turn a link on and off.
Autoupdate positions determines whether
or not the link will be updated if
either of the start or end transforms
are modified.
The navigation layer is the layer that
the off mesh link belongs to. For more
information on navigation layers, see
the lesson on nav mesh baking linked
below.
It is worth noting that once you have
set up your off mesh link component, you
do not need to re-ake your nav mesh. The
link will automatically be visible on
the nav mesh when you open the
navigation view.
We can now run our scene and see the
agent move across our newly created off
mesh link.