Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavWorld.GetResultFromFindPath

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public int GetResultFromFindPath(NavQueryBuffer queryBuffer, NativeSlice<NavNode> path);

Parameters

Parameter Description
queryBuffer The container that holds the path nodes obtained by a completed pathfinding query.
path The array to fill with the sequence of NavMesh nodes that comprises the found path.

Returns

int The number of path nodes copied into the provided array.

Description

Copies the NavMesh nodes that form the path found by the NavWorld operation into the provided array.

Call this method at the end of a successful NavWorld.BeginFindPath - NavWorld.ContinueFindPath - NavWorld.EndFindPath sequence to obtain the resulting path.

You can call it multiple times as long as NavWorld.BeginFindPath hasn't been called for that same query.

If the path stored in the query is longer than the provided array, this method copies only as many nodes as the array holds, starting from the beginning of the path.

Important: If a NavMesh modification removed the start node of the path since the initial BeginFindPath call of the pathfinding operation, the returned path is empty.

using Unity.Collections;
using UnityEngine;
using Unity.AI.Navigation.LowLevel;

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, 1024);
    }

    void Update()
    {
        NavLocation start = m_World.MapLocation(transform.position, Vector3.one, 0);
        NavLocation end = m_World.MapLocation(target.position, Vector3.one, 0);
        if (!m_World.IsValid(start) || !m_World.IsValid(end))
            return;

        NavQueryStatus status = m_World.BeginFindPath(m_Buffer, start, end);
        while ((status & NavQueryStatus.InProgress) != 0)
            status = m_World.ContinueFindPath(m_Buffer, 64, 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 copied = m_World.GetResultFromFindPath(m_Buffer, path);

        // The path is a corridor of nodes, not a list of waypoints. Draw the gate that each
        // pair of consecutive nodes shares to see the corridor the agent can move through.
        for (int i = 0; i < copied - 1; i++)
        {
            if (m_World.GetPortalPoints(path[i], path[i + 1], out Vector3 left, out Vector3 right))
                Debug.DrawLine(left, right, Color.yellow);
        }

        path.Dispose();
    }

    void OnDisable()
    {
        m_Buffer.Dispose();
        m_World.Dispose();
    }
}