Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavWorld.CreateLocation

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 NavLocation CreateLocation(Vector3 position, NavNode node);

Parameters

Parameter Description
position The world position of the NavLocation to create.
node The valid identifier of the NavMesh node.

Returns

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

When the specified node is invalid, the returned object has an empty and invalid node, and a position equal to Vector3.zero.

Description

Constructs a valid NavLocation from a position and a navigation node.

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.

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