使用 NGUI 后 raycast 不会命中对撞机?

raycast wont hit collider after using NGUI?

将主要 UI 框架转换为 NGUI 后,我们发现我们无法再使用以下代码使用对撞机命中对象,但我们不使用 NGUI:

private void checkRayCast()
    {
        if ((Input.GetMouseButtonDown(0)))

        {

            Debug.Log("shoot ray!!!");

            Vector2 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit2;

            if (Physics.Raycast(ray, out hit2, 100))
            {

                //this should hit a 3d collider
                Debug.Log("we may hit something");
                Debug.Log(hit2.collider.gameObject.tag);
            }

            RaycastHit2D[] hits = Physics2D.RaycastAll(point, Vector2.zero, 0f);
            if (hits.Length != 0)
            {
                //this should all 2d collider
                Debug.Log(" ohh, we hit something");

            }
            else
            {
                Debug.Log("you hit nothing john snow!!!");
            }
            //if (hit.transform.gameObject.GetComponent<Rigidbody2D>() != null)
            //{

            //}
        }

    }

而且我们发现我们再也打不到2d对撞机或3d对撞机了

这是目标对象的检查器:

编辑

在听从@programmer 的建议并将对撞机的大小调整到非常大之后,检测到命中(谢谢,@programmer)

但是对撞机被改得太大了,以至于无法制作场景。我们现在应该知道它应该有多大。

在四处挖掘之后我发现了这一点:

诀窍是我们在使用NGUI时应该使用UICamera.currentCamera而不是Camera.main来发射射线。

如果我们在游戏视图中点击此处发射射线:

如果我们添加一行:

Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.red, 2, true);

之后

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

我们应该在 3d 模式下看到场景中的实际光线:

请注意代表光线的红线,由于 Camera.main 具有不同的比例,它无法射到所需位置。

但是如果我们把carmera改成UICamera来发射光线,我们就可以发射出想要的光线了。

Ray ray = UICamera.currentCamera.ScreenPointToRay(Input.mousePosition);

希望能帮到同坑的小伙伴们。