Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


MoveLocation(NavLocation, Vector3, int)

Translates a NavMesh location to another position without losing contact with the surface.
Read time 2 minutesLast updated 22 days ago

Definition

public readonly NavLocation MoveLocation(NavLocation location, Vector3 destination, int areaMask = -1)

Parameters

location

The position to move across the NavMesh surface.

destination

The world position to move the location toward.

areaMask

A bitmask with a value of 1 at the indexes of the areas that can be traversed, and a value of 0 for areas that aren't traversable. The default value is NavMesh.AllAreas.

Returns

Type

Description

NavLocationA new location on the NavMesh placed as closely as possible to the specified
destination
position.

Remarks

This method returns the location on the NavMesh that is closest to the
destination
position and that also has a continuous connection on the NavMesh surface, through the allowed area types, all the way to the start position specified by the
location
parameter. If the
destination
position is outside the edges of the surface or of its allowed areas, this method returns a position at the edge. The search for a connected pathway is constrained to a circular area between the start and destination points. This constraint makes the operation fast, but it doesn't find a path that deviates widely to go around large obstacles.
The movement doesn't cross NavMesh Links.
You can safely call this operation while a separate pathfinding query is in progress on the same query buffer instance.
For more information about the area types that
areaMask
filters, refer to Areas and Costs.
Additional Resources: NavWorld.MoveLocations

Examples

using UnityEngine;using UnityEngine.AI;using Unity.AI.Navigation.LowLevel;public class MoveLocationExample : MonoBehaviour{ public Transform target; void Update() { using NavWorld world = NavWorld.GetDefaultWorld(); NavLocation start = world.MapLocation(transform.position, Vector3.one, 0); if (!world.IsValid(start)) return; NavLocation moved = world.MoveLocation(start, target.position, NavMesh.AllAreas); Debug.DrawLine(start.position, moved.position, Color.cyan); }}