sourcePosition | 示例查询的原点。 |
hit | 保留所生成位置的属性。 |
maxDistance | 在距离源位置的此段距离内采样。 |
areaMask | 遮罩,指定在查找最近的点时允许的导航网格区域。 |
bool 如果找到最近的点,则为 true。
在指定范围内找到导航网格上最近的点。
此函数对导航网格进行采样,以找到导航网格上最近的点。
基于到查询点的距离返回最近的点。此函数不检查环境中的障碍物。例如,如果源位置在天花板上,此函数将返回二楼的点(如果此处有导航网格)而不是一楼的地板位置。
如果搜索半径非常大,该函数的成本可能会非常高。要获得一个良好的起点,其最大距离应是代理高度的 2 倍。
如果您正在尝试查找导航网格上的随机点,最好使用建议的半径并多次尝试,而不是使用非常大的半径。
// 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 | 保留所生成位置的属性。 |
maxDistance | 在距离源位置的此段距离内采样。 |
filter | 一种过滤器,指定在查找最近的点时允许的导航网格区域。 |
bool 如果找到最近的点,则为 true。
在为过滤器指定的代理类型构建的任何导航网格上,对最接近源位置的位置进行采样。
仅考虑 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.