Author Topic: Raycast wont hit collider after using NGUI?  (Read 127 times)

armnotstrong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Raycast wont hit collider after using NGUI?
« on: June 15, 2017, 10:44:49 PM »
After turning 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:

  1. private void checkRayCast()
  2.     {
  3.         if ((Input.GetMouseButtonDown(0)))
  4.  
  5.         {
  6.  
  7.             Debug.Log("shoot ray!!!");
  8.  
  9.             Vector2 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  10.  
  11.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  12.             RaycastHit hit2;
  13.  
  14.             if (Physics.Raycast(ray, out hit2, 100))
  15.             {
  16.  
  17.                 //this should hit a 3d collider
  18.                 Debug.Log("we may hit something");
  19.                 Debug.Log(hit2.collider.gameObject.tag);
  20.             }
  21.  
  22.             RaycastHit2D[] hits = Physics2D.RaycastAll(point, Vector2.zero, 0f);
  23.             if (hits.Length != 0)
  24.             {
  25.                 //this should all 2d collider
  26.                 Debug.Log(" ohh, we hit something");
  27.  
  28.             }
  29.             else
  30.             {
  31.                 Debug.Log("you hit nothing john snow!!!");
  32.             }
  33.             //if (hit.transform.gameObject.GetComponent<Rigidbody2D>() != null)
  34.             //{
  35.  
  36.             //}
  37.         }
  38.  
  39.     }

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

What should we do to make this work? thanks in advance.

Awesome framework by the way 1000+ times usefully than the official UI component.

armnotstrong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Raycast wont hit collider after using NGUI?
« Reply #1 on: June 15, 2017, 10:48:53 PM »
And by the way here is the inspector setting if it helps to solve this problem


armnotstrong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Raycast wont hit collider after using NGUI?
« Reply #2 on: June 15, 2017, 11:38:30 PM »
After changing the collider size to a very big one, hit was detected, but Just don't know how big it should be here


Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 7
  • Posts: 216
    • View Profile
Re: Raycast wont hit collider after using NGUI?
« Reply #3 on: June 16, 2017, 02:20:03 PM »
There's a tick-box in the Widget settings, Collder - auto adjust to match. This scales the box collider to fit the sprite or widget dimensions. If you look at the UI in the Editor (double click UI root to centre on it) you can see the box colliders in green as you edit them too, so you can make them any size that suits and see how large they are in the interface. eg. I'll make the boxes bigger for labels on a touch screen. As a final note, the proportions of the collider are in UI pixels. If your UI is set to 1920x1080, say, then a collider of dimensions 100x100 will be about one tenth the screen height.

armnotstrong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Raycast wont hit collider after using NGUI?
« Reply #4 on: June 17, 2017, 01:10:41 AM »
Hey, thanks for your reply

 I have figured this out that I should use the `UICamera` instead of `Camera.main` to make this work, I have also posted the question and answer it myself on StackOverflow [https://stackoverflow.com/questions/44579727/raycast-wont-hit-collider-after-using-ngui], check it out :D