| Parameter | Description |
|---|---|
| position | The world position for which to find the closest point on the NavMesh. |
| extents | Maximum distance from the specified position, expanding along all three axes, within which Unity searches for NavMesh surfaces. |
| agentTypeId | The identifier for the agent type whose NavMesh surfaces this operation uses. |
| areaMask | Bitmask with values of 1 at the indices of areas that Unity samples, and values of 0 for areas that it ignores. The default value is NavMesh.AllAreas. |
NavLocation An object with position and valid NavNode if Unity finds a point on the NavMesh. An invalid object if Unity doesn't find a NavMesh surface with the desired features within the search area.
Finds the closest point and NavNode on the NavMesh for a given world position.
The search applies only to the specified type of NavMesh surface, for one or more desired area types and is limited to within the specified search area. It doesn't search for positions on NavMesh Links.
The Humanoid agent type exists for all NavMeshes and has an ID of 0. You can define other agent types manually in the Editor, and you must bake a separate NavMesh surface for each agent type.
Unity prefers nearby NavMesh surfaces directly above or below the specified position. When none exist up or down within the specified search extents, it samples the surfaces closest sideways.
Important: The returned position isn't aligned vertically to the HeightMesh, if one exists.
For more information about area types and the way they influence navigation, refer to Areas and Costs.
Additional resources: NavMesh.SamplePosition, NavWorld.IsValid
using UnityEngine; using UnityEngine.AI; using Unity.AI.Navigation.LowLevel; public class MapLocationExample : MonoBehaviour { void Update() { using NavWorld world = NavWorld.GetDefaultWorld(); // The extents reach 1 metre along each axis from the transform's position, // so the search covers a 2x2x2 metre box. The agent type ID 0 is the built-in Humanoid. NavLocation location = world.MapLocation(transform.position, Vector3.one, 0, NavMesh.AllAreas); if (world.IsValid(location)) Debug.DrawLine(transform.position, location.position, Color.green); } }