sourcePosition | 样本查询的原点。 |
hit | 持有结果位置的属性。hit.normal 的值永远不会被计算。它始终是 (0,0,0)。 |
maxDistance | 在从 sourcePosition 这一距离内采集样本。 |
areaMask | 查找到最近点时指定允许使用的 NavMesh 区域的遮罩。 |
找到最近点为bool True;
在指定范围内基于 NavMesh 查找最近点。
通过沿垂直轴将输入点投影到附近的 NavMesh 实例上来找到最近点。此垂直轴已在创建时为每个实例选择。如果此步骤未在指定距离内找到投影点,那么采样会扩展到周围的 NavMesh 位置。
根据离查询点的距离找到最近点。此函数不考虑阻碍。例如,在两层结构中,如果 sourcePosition 被设置为一楼天花板上的某点,则最近点可能位于二楼而不是一楼。天花板不被视为阻碍。
如果指定了较大的搜索半径,这个函数可能会降低帧率。为了避免帧速率问题,建议您指定一个等于代理高度的两倍的 maxDistance。
如果您尝试在 NavMesh 上找到一个随机点,您应该使用推荐半径并执行多次查找,而不是使用很大的半径。
// RandomPointOnNavMesh using UnityEngine; using UnityEngine.AI;
public class RandomPointOnNavMesh : MonoBehaviour { public float range = 10.0f;
bool RandomPoint(Vector3 center, float range, out Vector3 result) { for (int i = 0; i < 30; i++) { Vector3 randomPoint = center + Random.insideUnitSphere * range; NavMeshHit hit; if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas)) { result = hit.position; return true; } } result = Vector3.zero; return false; }
void Update() { Vector3 point; if (RandomPoint(transform.position, range, out point)) { Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f); } } }
sourcePosition | 样本查询的原点。 |
hit | 持有结果位置的属性。hit.normal 的值永远不会被计算。它始终是 (0,0,0)。 |
maxDistance | 在从 sourcePosition 这一距离内采集样本。 |
filter | 查找最近点时指定哪些 NavMesh 区域是允许访问的筛选器。 |
找到最近点为bool True;
在为 filter 指定的代理类型构建的任何 NavMesh 上对 sourcePosition 附近的最近位置进行采样。
仅考虑在 NavMeshQueryFilter.areaMask 中定义的区域上的位置。通过 maxDistance 设置最大的搜索半径。在 hit 参数中返回任何已找到位置的信息。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.