Using Navmesh Off Mesh Links
Unity Nav Mesh Links scripts are baked into the Quantum navmesh (Quantum.NavMesh.Links).
The following fields are used during baking:
Bidirectional- creates two links in each directionCostModifier- used as input forQuantum.NavMeshLink.CostOverrideArea Type- set the region of the link (see Toggling a Navmesh Link at Run-Time section)Activated- if not activated the link will be skipped during bakingGameObject.name- used as input for QuantumQuantum.NavMeshLink.Name
Creating a Navmesh Link
- Create and configure a Unity
NavMesh Link
- Bake the Quantum navmesh and verify the link by checking the gizmos for blue arrows (Quantum gizmo overlay menu, enable
NavMeshLinksand select theQuantumMapDataGameObject)
- Agents now considers the link for path-finding.
Toggling a Navmesh Link at Run-Time
Links can be toggled on and off during runtime using Quantum navmesh regions. They can be baked with a region id and toggled globally or per agent. Read the Regions Manual for more information.
Simple Region Mode
Choose the Area Type of the Unity NavMesh Link to set the region.
Advanced Region Mode
- Set the
Area Typeof the UnityNavMesh Linkto a region detection area. - Attach a
MapNavMeshRegionscript to the UnityNavMesh Link, set a regionIdand setCast RegiontoNo Region.
Customization and Signals
The link data structure can be queried from the navmesh asset using NavMesh.Links. If a mapping from link index to name or vice-versa is required, a dictionary can be constructed.
The NavMeshPathfinder component has the following Link API:
bool IsOnLink(FrameBase)- returnstruewhen the agent is currently on a linkint CurrentLink(FrameBase)- returns the current link index that points intoNavMesh.Linksor-1when currently not on a link
Waypoint have link related WaypointFlags:
LinkStart- this waypoint is the start of a linkLinkStop- this waypoint is the end of a linkRepathWhenReached- after reaching this waypoint the agent re-runs path finding
When setting a new target while the agent is on a link the agent will finish the current link before executing the path-finding (see WaypointFlag.RepathWhenReached).
No automatic re-pathing (for example for NavMeshAgentConfig.MaxRepathTimeout) will be executed as long as the agent is traversing a link.
By default the agent will traverse the link with its normal speed.
To take control of the agent movement when the link has been reached, the ISignalOnNavMeshWaypointReached signal can be used. Afterwards the agent can be disabled until an animation is complete or the movement can be overwritten in following ISignalOnNavMeshMoveAgent callbacks.
- Receiving any navigation callbacks requires enabled
SimulationConfig.Navigation.EnabledNavigationCallbacks - Receiving an
ISignalOnNavMeshMoveAgentcallback requires theNavMeshAgentConfig.MovementTypeto be set toCallback, it is possible to change the config of an agent at run-time.
The sample code performs a teleport when an agent steps on the link start waypoint.
C#
namespace Quantum
{
using Photon.Deterministic;
using UnityEngine.Scripting;
[Preserve]
public unsafe class NewQuantumSystem : SystemMainThread, ISignalOnNavMeshWaypointReached
{
public override void Update(Frame frame)
{
}
public void OnNavMeshWaypointReached(Frame frame, EntityRef entity, FPVector3 waypoint, Navigation.WaypointFlag waypointFlags, ref bool resetAgent)
{
var agent = frame.Get<NavMeshPathfinder>(entity);
var waypointIndex = agent.WaypointIndex;
if ((waypointFlags & Navigation.WaypointFlag.LinkStart) == Navigation.WaypointFlag.LinkStart)
{
// There always is another waypoint after the LinkStart
var linkDestination = agent.GetWaypoint(frame, waypointIndex + 1);
f.Unsafe.GetPointer<Transform2D>(entity)->Position = linkDestination.XZ;
}
}
}
}
Back to top