Select your preferred scripting language. All code snippets will be displayed in this language.
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.
CloseareaMask | A bitfield mask specifying which NavMesh areas can be passed when tracing the path. |
maxDistance | Terminate scanning the path at this distance. |
hit | Holds the properties of the resulting location. |
bool True if terminated before reaching the position at maxDistance, false otherwise.
Sample a position along the current path.
This function looks ahead a specified distance along the current path. Details of the mesh
at that position are then returned in a NavMeshHit object. This could be used, for example,
to check the type of surface that lies ahead before the character gets there - a character could
raise his gun above his head if he is about to wade through water, say.
Another common use-case is to probe the navmesh area directly under the agent.
#pragma strict @RequireComponent(NavMeshAgent) public class SampleArea { public var areaName = "Water"; var agent; var hit; var waterMask; function Start() { // Cache component and query mask agent = GetComponent.<NavMeshAgent>(); waterMask = GetAreaMaskFromName(areaName); } function Update() { // Use zero look-ahead distance to sample area directly under agent agent.SamplePathPosition(NavMesh.AllAreas, 0.0f, hit); if ((hit.mask & waterMask) != 0) { Debug.Log("Agent standing on: " + areaName); } } function GetAreaMaskFromName(name) { var area = NavMesh.GetAreaFromName(name); if (area == -1) { Debug.LogWarning("No navmesh area with name: " + name); return 0; } return 1 << area; } }
using UnityEngine; using System.Collections;
[RequireComponent (typeof (NavMeshAgent))] public class SampleArea : MonoBehaviour { public string areaName = "Water"; NavMeshAgent agent; NavMeshHit hit; int waterMask;
void Start () { // Cache component and query mask agent = GetComponent<NavMeshAgent> (); waterMask = GetAreaMaskFromName (areaName); } void Update () { // Use zero look-ahead distance to sample area directly under agent agent.SamplePathPosition (NavMesh.AllAreas, 0.0f, out hit); if ((hit.mask & waterMask) != 0) { Debug.Log ("Agent standing on: " + areaName); } }
int GetAreaMaskFromName (string name) { int area = NavMesh.GetAreaFromName (name); if (area == -1) { Debug.LogWarning ("No navmesh area with name: " + name); return 0; } return 1 << area; } }