ContinueFindPath(NavQueryBuffer, int, out int)
Continues a path search that is in progress.
Read time 2 minutesLast updated 8 hours ago
Definition
- Type: Method
- Namespace: Unity.AI.Navigation.LowLevel
- Assembly: UnityEngine.AIModule
ContinueFindPath(NavQueryBuffer, int, int)
public readonly NavQueryStatus ContinueFindPath(NavQueryBuffer queryBuffer, int nodesToVisit, out int nodesVisited)
Parameters
The container used to store intermediate node data for this search operation.
Maximum number of nodes to be traversed by the search algorithm during this call.
Outputs the actual number of nodes that have been traversed during this call.
Returns
Type | Description |
|---|---|
| NavQueryStatus | A bitfield with one of the following three main flags set: |
Remarks
The operation needs to have been initialized previously with NavWorld.BeginFindPath and it runs until the entire route is found or the specified number of iterations have been executed.
As long as the previous call returned a state of this method can be called repeatedly, across different frames, until the operation is successful. Use NavWorld.EndFindPath afterwards to prepare the path data for retrieval, along with the number of contained nodes.
InProgressAdditional Resources: NavQueryStatus.StatusDetailMask
Examples
using Unity.AI.Navigation.LowLevel;using Unity.Collections;using UnityEngine;public class FindPathExample : MonoBehaviour{ public Transform target; NavWorld m_World; NavQueryBuffer m_Buffer; void OnEnable() { m_World = NavWorld.GetDefaultWorld(); m_Buffer = new NavQueryBuffer(m_World, Allocator.Persistent, 64); } void Update() { if (target == null || !m_World.IsValid()) return; int anyAgentType = -1; NavLocation start = m_World.MapLocation(transform.position, 5f * Vector3.one, anyAgentType); int agentTypeAtStart = m_World.GetAgentTypeIdForNode(start.node); NavLocation end = m_World.MapLocation(target.position, 5f * Vector3.one, agentTypeAtStart); if (!m_World.IsValid(start) || !m_World.IsValid(end)) { Debug.DrawRay(transform.position, Vector3.up, Color.red); return; } NavQueryStatus status = m_World.BeginFindPath(m_Buffer, start, end); while ((status & NavQueryStatus.InProgress) != 0) status = m_World.ContinueFindPath(m_Buffer, 20, out int _); if ((status & NavQueryStatus.Success) == 0) return; status = m_World.EndFindPath(m_Buffer, out int pathSize); if ((status & NavQueryStatus.Success) == 0) return; NativeArray<NavNode> path = new NativeArray<NavNode>(pathSize, Allocator.Temp); int nCopied = m_World.GetResultFromFindPath(m_Buffer, path); DrawPath(path, nCopied); path.Dispose(); } void DrawPath(NativeArray<NavNode> path, int nodeCount) { NavLocation pathStart = m_World.CreateLocation(transform.position, path[0]); Vector3 previousMiddle = pathStart.position; // The path is a corridor of nodes, not a list of waypoints. Draw a path // through the middle of each gate shared by a pair of consecutive nodes. for (int i = 0; i < nodeCount - 1; i++) { if (m_World.GetPortalPoints(path[i], path[i + 1], out Vector3 left, out Vector3 right)) { Vector3 middle = 0.5f * (left + right); Debug.DrawLine(previousMiddle, middle, Color.yellow); previousMiddle = middle; } } NavLocation pathEnd = m_World.CreateLocation(target.position, path[nodeCount - 1]); Debug.DrawLine(previousMiddle, pathEnd.position, Color.yellow); } void OnDisable() { m_Buffer.Dispose(); m_World.Dispose(); }}