sourcePosition | 射线的原点。 |
targetPosition | 射线的末端。 |
hit | 保留射线投射生成位置的属性。 |
areaMask | 位域遮罩,指定在绘制射线时可通过的导航网格区域。 |
bool 如果射线在到达目标位置之前终止,则为 true。否则返回 false。
在导航网格上的两点之间找出一条线。
首先在导航网格上映射源点和目标点,然后从源点向着目标点的方向绘出一条射线。如果射线触及导航网格边界,则函数返回 true,并填充接触点数据。如果从源到目标的路径畅通无阻,则函数返回 false。
如果射线投射在外边缘上终止,则 hit.mask
为 0;否则它包含阻挡多边形部分的区域遮罩。
该函数可用于检查代理是否可以在导航网格上的两点之间畅通无阻地行走。例如,如果您的角色拥有需要空间的逃避躲闪行动,您可以从角色位置向多个方向发射射线,以找到角色可以躲闪的位置。
NavMesh.Raycast 与物理射线投射不同,因为它适用于导航网格上的“2.5D”。与物理射线投射不同的是,导航网格版本可以检测到各种类型的导航障碍物(如地面上的孔洞),并且如果该区域可导航,它也可以向上爬坡。
// TargetReachable using UnityEngine; using UnityEngine.AI;
public class TargetReachable : MonoBehaviour { public Transform target; private NavMeshHit hit; private bool blocked = false;
void Update() { blocked = NavMesh.Raycast(transform.position, target.position, out hit, NavMesh.AllAreas); Debug.DrawLine(transform.position, target.position, blocked ? Color.red : Color.green);
if (blocked) Debug.DrawRay(hit.position, Vector3.up, Color.red); } }
如果您正在寻找 NavMesh 上最近的点,您应该使用物理射线投射找到环境中的点,请参阅 Move to Click Point 示例。
sourcePosition | 射线的原点。 |
targetPosition | 射线的末端。 |
hit | 保留射线投射生成位置的属性。 |
filter | 一种过滤器,指定在绘制射线时可通过的导航网格区域。 |
bool 如果射线在到达目标位置之前终止,则为 true。否则返回 false。
在导航网格上的两点之间找出一条线,受过滤器参数定义的限制条件的约束。
这条线终止于外边缘或不可通过的区域。
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.