在凹面MeshCollider上的Raycast

时间:2018-01-20 15:54:49

标签: unity3d raycasting collider

所以我试图检测我的播放器相机是在凹面对撞机(管道)的内部还是外部。我目前的方法如下:

 void Update () {
    int layerMask = 0x1 << LayerMask.NameToLayer("Tube");
    int maxLen = 1000;
    var camDir = cam.transform.forward;

    RaycastHit hit1;
    if (Physics.Raycast(cam.transform.position, camDir, out hit1, maxLen, layerMask))
    {
        //Camera is either inside or outside the tube
        Debug.DrawLine(cam.transform.position, hit1.point, Color.green);

        RaycastHit hit2;
        if (Physics.Raycast(hit1.point + camDir, camDir, out hit2, maxLen, layerMask))
        {
            //Ray hits concave tube twice (outside and inside)
            //So camera is definitely outside.
            Debug.DrawLine(hit1.point + camDir, camDir, Color.cyan);
            Debug.Log("Camera is outside!");
        }else
        {
            Debug.Log("Camera is inside!");
        }
    }    
}

理论上,如果两条光线碰撞,那么我们就在管外。 (外侧碰撞一次,内侧碰撞一次)。但不知何故,Raycast忽略了对撞机的外部并且直接通过。有什么办法可以阻止这个或者更好的解决方案吗? Note: green line is ray

0 个答案:

没有答案