Version: 2020.3

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... } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961