Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavLocation

struct in Unity.AI.Navigation.LowLevel

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

Description

A world position that is guaranteed to be on the navigation data, either on a NavMesh surface or on a link.

The NavLocation stores that position together with the NavNode of the node containing it, which is either a polygon of a NavMesh surface or a link. Using NavLocations with NavWorld operations removes the need to project the desired world position onto the NavMesh at the beginning of each operation.

A NavLocation can be invalid in the following scenarios:

  1. When it has been created empty, instead of being the result of a NavWorld operation.
  2. When a NavWorld.MapLocation call finds no polygon that matches the requested position, extents, agent type, or area mask.
  3. When the NavMesh has been removed or modified at the indicated position or in its close vicinity.

If a NavMeshObstacle carving the NavMesh in its vicinity makes a NavLocation invalid, the NavLocation returns to a valid state once all NavMeshObstacle objects stop carving the NavMesh tile where the NavLocation is. This is because removing all NavMeshObstacle objects restores the NavMesh to its original form without regenerating it.

Additional resources: NavWorld.MapLocation, NavWorld.IsValid, NavNode, NavNodeType

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

public class NavLocationExample : MonoBehaviour
{
    public Transform target;

    void Update()
    {
        using NavWorld world = NavWorld.GetDefaultWorld();

        // Obtain a NavLocation by projecting a world position onto the NavMesh.
        NavLocation here = world.MapLocation(transform.position, Vector3.one, 0);
        if (!world.IsValid(here))
            return;

        // The location pairs a snapped world position with the node that owns it.
        Debug.DrawLine(transform.position, here.position, Color.green);

        // Pass the same NavLocation to other NavWorld operations without re-projecting.
        NavLocation reached = world.MoveLocation(here, target.position, NavMesh.AllAreas);
        Debug.DrawLine(here.position, reached.position, Color.cyan);
    }
}

Properties

Property Description
nodeThe unique identifier for the node in the NavMesh to which the world position has been mapped.
positionA world position that sits precisely on the surface of the NavMesh or along its links.

Public Methods

Method Description
EqualsChecks whether two NavLocation objects represent the same position on the same NavMesh node.
GetHashCodeReturns the hash code for use in collections.

Operators

Operator Description
operator !=Checks whether two NavLocation objects differ in position or refer to different NavMesh nodes.
operator ==Checks whether two NavLocation objects have the same position and refer to the same NavMesh node.