Legacy Documentation: Version 5.0
Language: English
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

public function SamplePathPosition(areaMask: int, maxDistance: float, out hit: NavMeshHit): bool;
public bool SamplePathPosition(int areaMask, float maxDistance, out NavMeshHit hit);

Parameters

areaMaskA bitfield mask specifying which NavMesh areas can be passed when tracing the path.
maxDistanceTerminate scanning the path at this distance.
hitHolds the properties of the resulting location.

Returns

bool True if terminated before reaching the position at maxDistance, false otherwise.

Description

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