Version: 2023.2
public static bool Raycast (Vector3 sourcePosition, Vector3 targetPosition, out AI.NavMeshHit hit, int areaMask);

参数

sourcePosition 射线的原点。
targetPosition 射线的末端。
hit 保留射线投射生成位置的属性。
areaMask 位域遮罩,指定在绘制射线时可通过的导航网格区域。

返回

bool 如果射线在到达目标位置之前终止,则为 true。否则返回 false。

描述

在导航网格上的两点之间找出一条线。

首先在导航网格上映射源点和目标点,然后从源点向着目标点的方向绘出一条射线。如果射线触及导航网格边界,则函数返回 true,并填充接触点数据。如果从源到目标的路径畅通无阻,则函数返回 false。

如果射线投射在外边缘上终止,则 hit.mask 为 0;否则它包含阻挡多边形部分的区域遮罩。

该函数可用于检查代理是否可以在导航网格上的两点之间畅通无阻地行走。例如,如果您的角色拥有需要空间的逃避躲闪行动,您可以从角色位置向多个方向发射射线,以找到角色可以躲闪的位置。

The Raycast is different from physics ray cast because it works on “2.5D”, on the NavMesh. The difference to physics ray casts is that NavMesh ray casts can detect all kinds of navigation obstructions, such as holes in the ground, and it can also climb up slopes, if the area is navigable.

// 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); } }

If you want to find the nearest point on the NavMesh, use physics ray cast to find a point in the world. For more information, refer to the Move to Click Point example.


public static bool Raycast (Vector3 sourcePosition, Vector3 targetPosition, out AI.NavMeshHit hit, AI.NavMeshQueryFilter filter);

参数

sourcePosition 射线的原点。
targetPosition 射线的末端。
hit 保留射线投射生成位置的属性。
filter 一种过滤器,指定在绘制射线时可通过的导航网格区域。

返回

bool 如果射线在到达目标位置之前终止,则为 true。否则返回 false。

描述

在导航网格上的两点之间找出一条线,受过滤器参数定义的限制条件的约束。

这条线终止于外边缘或不可通过的区域。

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