0

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)
            //{

            //}
        }

    }

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

Here is the target object's inspector: enter image description here

EDIT

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 sceneenter image description here note the green border which indicts the collider size made scene

  • here is the one that the collider works, but should be unreasonably large:enter image description here

Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • I don't know why you switched to NGUI from UGUI. It's fine to use NGUI if your project is built on top of NGUI but don't create new projects on top of NGUI. Not only do you get more support with UGUI, Unity 2017 will add integrate [TextMesh Pro](https://www.assetstore.unity3d.com/en/#!/content/84126) to UGUI which even makes it even much more better. It would've been fine to use NGUI 3 years ago. – Programmer Jun 16 '17 at 04:02
  • Sorry for the rant but it's not too late to re-evaluate that decision. I noticed so many `Debug.Log` in the code....Do you see any log at-all? What happens when you increase the size of the Collider ? Also try disabling *Is Trigger*. Haven't used NGUI for a long time but what happens when click the "Collider" check box on the `UISprite` script? It should adjust the Collider. Just throwing out some possible solution to you. – Programmer Jun 16 '17 at 04:05
  • @Programmer thanks for reply the reason we change to NGUI is that we meet a lot of pits when trying to use that, one big problem is the performance issue that when texture overlaps with alpha, and clearly they didn't do a good job about that, and another thing is that it's very not convenient to combine the UGUI and the game object in the world, like I want to drag something from UI Panel to the game world. So we decide to change to NGUI until UGUI could do the job. And I will try to increase the collider a bit more to check if the NGUI's size adjustment has caused this problem. – armnotstrong Jun 16 '17 at 04:21
  • And there is Log printed which indicts that ray cast just not hit anything, either 3d or 2d collider won't work. – armnotstrong Jun 16 '17 at 04:24
  • Your first comment makes sense. As ffor your second comment, you can put a ui object in world space by changing the Canva's render mode to World space. Do you see ` Debug.Log("shoot ray!!!");` log? Also did you resize the Collider to see what happens? – Programmer Jun 16 '17 at 04:26
  • hmm, yes there is "shoot ray!!!" printed, and resize the collider is on the way, will come back after the test to see what happens. – armnotstrong Jun 16 '17 at 04:29
  • @Programmer good news! after resizing the collider to a very big one, hit was detected. Now I just have to calculate how big the collider should be, and it may be a very straight forward job to do :-( – armnotstrong Jun 16 '17 at 04:32
  • Yeah there is a limit on how small a collider can be. Maybe you just need to re-size the z axis since you are using 3D collider. You need to do this in the scene view as that makes it easier to see what you are resizing. – Programmer Jun 16 '17 at 04:38
  • @Programmer check the EDIT, to make the hit detected, I have to make the collider unreasonably large :D – armnotstrong Jun 16 '17 at 04:48
  • @Programmer and I tried both 2d/3d collider, both should be enormously large to make the hit happen. – armnotstrong Jun 16 '17 at 04:51
  • Is the camera or the parent object rotated? Also change the `0` in `Physics2D.RaycastAll(point, Vector2.zero, 0f);` to larger number. Let's say 200. – Programmer Jun 16 '17 at 05:08
  • And if that doesn't work, maybe your camera is zoomed in too much? Either the camera field of view or the z axis is zoomed in too much. Zoom out so that the images are really small then resize the image after zooming out. The collider should then resize too. This will likely fix your problem. – Programmer Jun 16 '17 at 05:48
  • Hi @Programmer, I have figured this out that I should use the UICamera to shoot the ray instead of the main camera to make things work – armnotstrong Jun 16 '17 at 10:12
  • But the raycast should work on the collider regardless even without any UI. Anyways, glad you figured it out. – Programmer Jun 16 '17 at 10:16
  • I see. Good job – Programmer Jun 17 '17 at 06:21

1 Answers1

1

After digging around And I have figured this out:

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:enter image description here

And If we add a line like:

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

After

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

We should see the actual ray in the scene in a 3d mode: enter image description here

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

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); enter image description here

Hope this could help guys meet with the same pit.

armnotstrong
  • 8,605
  • 16
  • 65
  • 130