Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavWorld.GetInstanceTransform

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 void GetInstanceTransform(NavNode node, out Vector3 position, out Quaternion rotation);

Parameters

Parameter Description
node The NavMesh node whose owner's transform to retrieve.
position Outputs the world position of the NavMesh surface that owns the specified node.

This is a zero vector when the NavMesh node is a NavMesh Link.
rotation Outputs the world rotation of the NavMesh surface that owns the specified node.

This is Quaternion.identity when the NavMesh node is a NavMesh Link.

Description

Retrieves the position and rotation of the NavMesh surface that contains the specified NavMesh node.

Unity defines the transform of a NavMeshData surface from the position and rotation values that you declare when you bake the surface with NavMeshBuilder.BuildNavMeshData, or as part of a NavMesh Surface, or when you explicitly set the values for NavMeshData.position and NavMeshData.rotation.

You can also specify custom transforms for NavMeshDataInstances when you create them, by passing explicit position and rotation values to the NavMesh.AddNavMeshData method.

Important: This method doesn't return the position and orientation of a single NavMesh polygon. It returns the position of the surface that owns the polygon.

Note: This method returns zero and identity instead of the actual transform for NavMesh Links that you instantiate with a call to NavMesh.AddLink.

Additional resources: NavWorld.GetNodeType

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

public class InstanceTransformExample : MonoBehaviour
{
    void Update()
    {
        using NavWorld world = NavWorld.GetDefaultWorld();
        NavLocation location = world.MapLocation(transform.position, Vector3.one, 0);
        if (!world.IsValid(location))
            return;

        world.GetInstanceTransform(location.node, out Vector3 surfacePosition, out Quaternion surfaceRotation);
        Debug.DrawRay(surfacePosition, surfaceRotation * Vector3.up, Color.yellow);
    }
}