Version: 2017.2

NavMeshAgent.SamplePathPosition

切换到手册
public bool SamplePathPosition (int areaMask, float maxDistance, out AI.NavMeshHit hit);

参数

areaMask 位域遮罩,指定在跟踪路径时可以通过哪些导航网格区域。
maxDistance 在此距离终止扫描路径。
hit 保留所生成位置的属性。

返回

bool 如果在到达 maxDistance 位置之前终止,则为 true,否则为 false。

描述

沿着当前路径对某一位置进行取样。

此函数将沿当前路径向前查找指定的距离。稍后会在 NavMeshHit 对象中返回该位置网格的详细信息。这可用于在角色到达之前检查 前方地面类型等 - 比如说,如果角色即将涉水前进,可以把枪举过头顶 。

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 &amp; waterMask) != 0) { // Water detected along the path... } } }