使用NGUI后,raycast不会撞到对撞机吗? [英] raycast wont hit collider after using NGUI?

查看:140
本文介绍了使用NGUI后,raycast不会撞到对撞机吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将主要的UI框架转换为NGUI之后,我们发现使用以下代码无法再通过碰撞器击中对象,而在不使用NGUI的情况下,该代码运行良好:

after turn the main UI framework to NGUI, we found that We couldn't hit object with collider anymore with following code which was working fine we not using 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对撞机

And we found that we could not hit a 2d collider or 3d collider anymore

这是目标对象的检查器:

Here is the target object's inspector:

在遵循@programmer的建议并将对撞机的尺寸调整为一个很大的对撞机之后,检测到命中(感谢@programmer)

After following @programmer 's advice and resize the collider to a very big one, hit was detected(thanks, @programmer)

但是对撞机被改变得太大了,以至于它不能成为场景.而且我们应该发现现在应该有多大.

But the collider was changed to so big that it not event make scene. And We should find how big this should be now.

  • 调整场景中对撞机的大小之前 请注意指示对撞机大小场景的绿色边框

  • before resizing the collider in the scene note the green border which indicts the collider size made scene

这是对撞机起作用的,但应该大得不合理:

here is the one that the collider works, but should be unreasonably large:

推荐答案

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

After digging around And I have figured this out:

诀窍在于,使用NGUI时,我们应该使用UICamera.currentCamera而不是Camera.main来拍摄射线.

The trick is we should use UICamera.currentCamera instead of Camera.main to shoot the ray when we using NGUI.

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

If we click here in the game view to shoot the ray:

如果我们添加如下一行:

And If we add a line like:

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

之后

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

我们应该以3d模式查看场景中的实际光线:

We should see the actual ray in the scene in a 3d mode:

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

Note the red line which represents the ray and It could not shoot at the desired position since Camera.main have a different scale.

但是,如果我们将Carmera更改为UICamera来拍摄射线,则可以拍摄所需的射线.

But if we change the carmera to UICamera to shoot the ray, We could shoot the ray that was desired.

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

希望这可以帮助人们在同一个坑中相遇.

Hope this could help guys meet with the same pit.

这篇关于使用NGUI后,raycast不会撞到对撞机吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆