Author Topic: Raycast hit and Camera debug last hit  (Read 3733 times)

gaurav.maniar

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
Raycast hit and Camera debug last hit
« on: August 19, 2014, 08:54:41 AM »
after enabling the "Debug" in camera, we are getting Last Hit object name.

on Mouse Click event object_name we get by Raycast hit, will be same as the object by Last Hit of Camera????

  1. if (Input.GetMouseButtonDown (0))
  2. {
  3.         RaycastHit hit;
  4.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  5.         if (Physics.Raycast (ray, out hit))
  6.         {
  7.                 if (hit.collider != null)
  8.                 {
  9.                         string object_name = hit.collider.name;
  10.                 }
  11.         }
  12. }
  13.  

I think that, it should be same, but i'm not getting same object.

-Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Raycast hit and Camera debug last hit
« Reply #1 on: August 19, 2014, 01:46:39 PM »
UICamera.lastHit is the result of the last raycast hit performed by NGUI. If you have more than one camera, which is most often the case with UI, then this is the result of a raycast done by the camera that hit the object. In case of interaction with UI elements, this will be the UI camera, not Camera.main.

If you need to know what's underneath the mouse, you should be looking at UICamera.hoveredObject instead. Also note that majority of NGUI's events are only valid during NGUI's callbacks such as OnHover, OnPress, etc.

gaurav.maniar

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Raycast hit and Camera debug last hit
« Reply #2 on: August 21, 2014, 12:37:46 AM »
my vertical scroll view structure,

added the invisible widget to scroll it outside from the objects added to scroll view (according to your video tutorial)

Panel (depth 1)
|>Container (invisible widget) (with box collider and UIDrag scroll view)

UIScroll View (depth 2)
|>Grid
    |>2d sprite (box collider and UIDrag scroll view)
        |>2d sprite
    |>2d sprite (box collider and UIDrag scroll view)
        |>2d sprite

and more 10 sprite elements in same way.

  1. if (Input.GetMouseButtonDown (0))
  2. {
  3.     RaycastHit hit;
  4.     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  5.     if (Physics.Raycast (ray, out hit))
  6.     {
  7.         if (hit.collider != null)
  8.         {
  9.             Debug.Log (hit.collider.name);
  10.         }
  11.     }
  12. }

i have enabled Debug option in Camera.

Last Hit showing every component properly if it is sprite or container,
but the problem is, in code by raycast hit, for some sprite objects i'm getting hit but for some sprite objects i'm getting hit on container.

3-4 times created new scroll view, but problem remains as it is, only objects on which i was getting hit are changing.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Raycast hit and Camera debug last hit
« Reply #3 on: August 22, 2014, 03:12:00 AM »
NGUI doesn't simply do a raycast, it also sorts entries by depth of their widgets. You aren't doing it.

Any reason you aren't simply sticking to UICamera.lastHit / UICamera.hoveredObject? Why are you doing your own raycasts?