Author Topic: Raycast from NGUI sprite to 3D object  (Read 6975 times)

justinISO

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 52
    • View Profile
Raycast from NGUI sprite to 3D object
« on: July 13, 2016, 01:08:01 PM »
Hey guys,
I am having trouble getting my raycasts to work correctly. I have a sprite that I move around the screen with a controller that acts as a reticle and I need to know if the reticle is over a 3D object. I have used many variations around this:

  1.             Ray ray = UICamera.mainCamera.ScreenPointToRay(reticle.position);
  2.             RaycastHit hit;
  3.  
  4.             if (Physics.Raycast(ray, out hit))
  5.             {
  6.                 Debug.Log("Hit");
  7.             }
  8.  

But I just can't seem to get it to work.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Raycast from NGUI sprite to 3D object
« Reply #1 on: July 15, 2016, 11:37:41 PM »
First, you are raycasting into all layers, when you should be selective instead.

Second, there is no need to raycast at all. Attach a UICamera script to your game camera, and you will then be able to get the object under the mouse by checking UICamera.lastHit. All your game objects will also receive NGUI events as an added bonus -- clicks, touches, all that.

justinISO

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 52
    • View Profile
Re: Raycast from NGUI sprite to 3D object
« Reply #2 on: July 19, 2016, 09:17:16 AM »
I am not checking under the mouse though. I have a reticle that is moved around the screen by a controller and I need to check under the reticle sprite and not the mouse.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Raycast from NGUI sprite to 3D object
« Reply #3 on: July 20, 2016, 03:20:58 AM »
Then my suggestion about being selective in your raycasts stands. Also UICamera.mainCamera will point to your UI camera and I am guessing you want to use your game's camera instead. Try Camera.main.ScreenPointToRay instead.