实例代码:
//射线
//参数一 起点位置
//参数二 方向
//参数三 碰撞信息
//参数四 检测多远的距离
RaycastHit hit;
if (Physics.Raycast(transform.position + Vector3.up * 0.3f, transform.forward, out hit, 4f))
{
//如果前方有物体 并且物体的tag == "Obstacle"
if (hit.collider.tag == "Obstacle")
{
//hit.distance射线的距离
if (hit.distance >= 3f && hit.distance <= 3.3f)
{
//匹配墙的位置 hit.point碰撞点的位置
//point 匹配的目标点
Vector3 point = hit.point;
point.y = hit.collider.transform.position.y + hit.collider.bounds.size.y + 0.5f;
matchWall = point;
isVault = true;
}
}
}