Navmesh
1.NavMesh.Raycast
public static bool Raycast (Vector3 sourcePosition, Vector3 targetPosition, out AI.NavMeshHit hit, int areaMask);
1.sourcePosition:射线的原点。
2.targetPosition:射线的末端。
3.hit:保留射线投射生成位置的属性
4.areaMask:位域遮罩,指定在绘制射线时可通过的导航网格区域。
返回
bool 如果射线在到达目标位置之前终止,则为 true。否则返回 false。
**
2.NavMeshHit
- distance 到击中点的距离。
- hit 击中时的标志集。
- mask 用于指定击中点的导航网格区域的遮罩。
- normal 击中点的法线。
- position 击中位置。
**
当开始位置与目标位置之间存在障碍物时,射线变红;
当开始位置与目标位置之间不存在障碍物时,射线变绿;
该方法NavMesh.Raycast可以检查障碍物的存在
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);Debug.Log(blocked);if (blocked)Debug.DrawRay(hit.position, Vector3.up, Color.red);print($"到击中点的距离:{hit.distance};击中时的标志集:{hit.hit};" +$"用于指定击中点的导航网格区域的遮罩:{hit.mask};击中点的法线:" +$"{hit.normal};击中位置:{hit.position}");}
结果演示:
链接:https://pan.baidu.com/s/1qOLLiQ2m0tOmZg1gw4BIfw
提取码:3gsk