使用NGUI后,raycast不会碰到对手吗?

问题描述 投票:0回答:1

在将主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)
            //{

            //}
        }

    }

我们发现我们再也无法击中二维对撞机或三维对撞机了

这是目标对象的检查器:enter image description here

编辑

按照@programmer的建议并将对撞机调整到一个非常大的对象后,检测到命中(谢谢,@ programmer)

但是对撞机变得如此之大,以至于事件不会成为场景。我们应该发现现在应该有多大。

  • 在调整sceneenter image description here中对撞机的大小之前,请注意绿色边框,指示对撞机尺寸为场景
  • 这是对撞机工作的那个,但应该是不合理的大:enter image description here
c# unity3d raycasting ngui
1个回答
1
投票

在挖掘之后我已经想到了这个:

诀窍是当我们使用NGUI时,我们应该使用UICamera.currentCamera而不是Camera.main拍摄光线。

如果我们在游戏视图中单击此处拍摄光线:enter image description here

如果我们添加如下行:

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

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

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

请注意代表光线的红线,由于Camera.main具有不同的比例,因此无法在所需位置拍摄。

但是,如果我们将相机更换为相机以拍摄当天,我们可以拍摄所需的光线。

Ray ray = UICamera.currentCamera.ScreenPointToRay(Input.mousePosition); enter image description here

希望这可以帮助人们遇到同样的坑。

© www.soinside.com 2019 - 2024. All rights reserved.