Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: justinISO on July 13, 2016, 01:08:01 PM

Title: Raycast from NGUI sprite to 3D object
Post by: justinISO 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.
Title: Re: Raycast from NGUI sprite to 3D object
Post by: ArenMook 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.
Title: Re: Raycast from NGUI sprite to 3D object
Post by: justinISO 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
Title: Re: Raycast from NGUI sprite to 3D object
Post by: ArenMook 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.