Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

NavMeshAgent.SamplePathPosition

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 Boolean SamplePathPosition(Int32 areaMask, Single maxDistance, out UnityEngine.AI.NavMeshHit hit);

Parameters

Parameter Description
areaMask 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.

Returns

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

Description

Sample a position along the current path.

This function looks ahead the specified maxDistance along the current path, up to the third corner. It returns details of the mesh at that position in a NavMeshHit object. You can use this to check the type of surface that lies ahead before the character gets there. For example, characters could raise their guns above their heads if they are about to wade through water.

If the path sampling terminates on an outer edge, hit.mask is 0. If the path sampling terminates at an area not specified by areaMask, hit.mask contains the area mask of the blocking polygon. If the sampling reaches the end of the path, or the limit at the path's third corner, hit.mask contains the area mask at that position on the NavMesh.

using UnityEngine;
using UnityEngine.AI;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform target; private NavMeshAgent agent; private int waterMask;

void Start() { agent = GetComponent<NavMeshAgent>(); waterMask = 1 << NavMesh.GetAreaFromName("Water"); agent.SetDestination(target.position); }

void Update() { NavMeshHit hit;

// Check all areas one length unit ahead. if (!agent.SamplePathPosition(NavMesh.AllAreas, 1.0F, out hit)) if ((hit.mask & waterMask) != 0) { // Water detected along the path... } } }