4.4 KiB
Creating a NavMesh Link
NavMesh Links are used to create paths crossing outside the walkable navigation mesh surface. For example, jumping over a ditch or a fence, or opening a door before walking through it, can be all described as NavMesh links.
You can define the start and end points of the link by placing two game objects in the Scene and adding a NavMesh Link component to one of them. The NavMesh Link will then create a navigation connection that the NavMesh Agent can follow.
We’re going to add an NavMesh Link component to describe a jump from the upper platform to the ground.
- First create two cylinders: Game Object > 3D Object > cylinder.
- You can scale the cylinders to (0.1, 0.5, 0.1) to make it easier to work with them.
- Move the first cylinder to the edge of the top platform, close to the NavMesh surface.
- Place the second cylinder on the ground, close to the NavMesh, at the location where the link should land.
- Select the first cylinder and add an NavMesh Link component to it. Choose Add Component from the inspector and choose Navigation > NavMesh Link.
- Assign the first cylinder in the Start Transform field and the second cylinder in the End Transform field.
Now you have a functioning NavMesh Link set up! Pathfinding will return the path going through the NavMesh link if that path is shorter than via walking along the NavMesh.
You can use any game object in the Scene to hold the NavMesh link component, for example a fence prefab could contain the NavMesh link component. Similarly you can use any game object with a Transform as the start and end marker.
The NavMesh bake process can detect and create common jump-across and drop-down links automatically. Take a look at the settings of NavMesh Surface for more details.
How to troubleshoot a link that does not work
If the agent does not traverse a NavMesh Link make sure that both end points are connected correctly to the NavMesh. To check the state of the connection make sure to enable the Show NavMesh debug visualization in the AI Navigation overlay. When the link has no width, a properly connected end point shows a circle around the access point in the scene view. If the link has width, the link shows a dark segment on the edge that connects properly to the NavMesh, or a gray line if the edge does not connect to the NavMesh. If both ends connect to the NavMesh, the wide link shows an additional solid transparent rectangle that fills the space between the link edges. The NavMesh link also shows an arc line between the ends, with an arrow at each end where the agent can exit the link. The arc line is colored black if at least one end is connected, or it is colored gray if none of the ends is connected to the NavMesh.
No agent or path can traverse a link that has the Activated property disabled. In that situation the link shows in the scene with a red color. Make sure to enable the Activated property when you want agents to be able to move through the link.
Another common cause of why an agent does not traverse a NavMesh Link is that the NavMesh Agent’s Area Mask does not include the NavMesh Link’s area type.
Additional resources
- NavMesh Link component reference
- Navigation HowTos - common use cases for NavMesh Agent, with source code.
- Sample 7 - Dungeon – an example of NavMesh links connecting NavMeshes at runtime.
- NavMesh Link scripting reference - full description of the NavMesh Link scripting API.