Unity在物体内部发射射线检测不到物体的解决方式
两种方式,建议采用代码,修改设置可能会在其他使用射线的地方鬼畜
使用代码…
/// <summary>
/// 射线碰撞检测,正向与反向检测
/// </summary>
private bool CheckCloud()
{
RaycastHit hit;
Ray rayForward = new Ray(LinBroth.transform.position, LinBroth.transform.TransformDirection(Vector3.forward));
if (Physics.Raycast(rayForward, out hit, 3000))
{
Debug.DrawRay(LinBroth.transform.position, LinBroth.transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit+正向射线");
return true;
}
//反向射线
Ray rayback = Reverse(rayForward, 3000);
if (Physics.Raycast(rayback, out hit, 3000))
{
Debug.DrawRay(LinBroth.transform.position, LinBroth.transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit+反向射线");
return true;
}
return false;
}
/// <summary>
/// 反转射线
/// </summary>
public Ray Reverse(Ray ray, float distance)
{
return new Ray(
ray.origin + ray.direction * distance,
-ray.direction);
}
修改设置
在Unity2018.1.18上不大好用。