Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


CreateLocation(Vector3, NavNode)

Constructs a valid NavLocation from a position and a navigation node.
Read time 1 minuteLast updated 18 days ago

Definition

public readonly NavLocation CreateLocation(Vector3 position, NavNode node)

Parameters

position

The world position of the NavLocation to create.

The valid identifier of the NavMesh node.

Returns

Type

Description

NavLocationAn object that contains the specified NavMesh node, if valid, and a position located on that node.

Remarks

When the specified node is a polygon, the returned position is the point on the surface of the polygon that is closest to the specified position.
When the specified node is a link, the returned position is the point on the link's end segments that is closest to the specified position. The position lies on the contact line between that segment of the link and a NavMesh surface.
Important: The returned position isn't aligned vertically to the HeightMesh, if one exists.
To obtain reliable positions on the NavMesh, you can also use NavWorld.MapLocation, NavWorld.MoveLocation, and NavWorld.GetPortalPoints.

Examples

using UnityEngine;using Unity.AI.Navigation.LowLevel;public class CreateLocationExample : MonoBehaviour{ void Update() { using NavWorld world = NavWorld.GetDefaultWorld(); // Map a world position to obtain a valid NavNode for the current polygon. NavLocation mapped = world.MapLocation(transform.position, Vector3.one, 0); if (!world.IsValid(mapped)) return; // Re-create a location at a different point that lies on the same polygon. Vector3 offset = transform.position + new Vector3(0.5f, 0f, 0f); NavLocation custom = world.CreateLocation(offset, mapped.node); Debug.DrawLine(transform.position, custom.position, Color.cyan); }}