NavLocation
A world position that is guaranteed to be on the navigation data, either on a NavMesh surface or on a link.
Read time 2 minutesLast updated 17 days ago
Definition
- Type: Struct
- Namespace: Unity.AI.Navigation.LowLevel
- Assembly: UnityEngine.AIModule
- Implements: IEquatable<NavLocation>
public readonly struct NavLocation : IEquatable<NavLocation>
Remarks
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:
- When it has been created empty, instead of being the result of a NavWorld operation.
- When a NavWorld.MapLocation call finds no polygon that matches the requested position, extents, agent type, or area mask.
- 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.
Examples
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 |
|---|---|
| node | The unique identifier for the node in the NavMesh to which the world position has been mapped. |
| position | A world position that sits precisely on the surface of the NavMesh or along its links. |
Methods
Method | Description |
|---|---|
| Equals | Checks whether two NavLocation objects represent the same position on the same NavMesh node. |
| GetHashCode | Returns the hash code for use in collections. |
Operators
Operator | Description |
|---|---|
| operator == | Checks whether two NavLocation objects have the same position and refer to the same NavMesh node. |
| operator != | Checks whether two NavLocation objects differ in position or refer to different NavMesh nodes. |