Author Topic: Sprite that follows mouse/touch position  (Read 8970 times)

ricardo

  • Guest
Sprite that follows mouse/touch position
« on: November 01, 2012, 12:52:03 PM »
Hi,

I've been trying to get this to work unsuccessfully.

I have a sprite in my NGUI Scene that I want to match to my mouse/touch position. My NGUI Scene is an additively loaded scene that contains the GUI and a Camera. This is loaded by every Scene in my game when the level starts.

What I'm doing is taking the mouse/touch positon (which is in screen space) and using the GUI's camera to transform that position Screen->Viewport, then using the viewport position to set the position of the NGUI sprite.

  1. Vector3 realPosition = HUDCamera.ScreenToViewportPoint(mouseScreenPosition);
  2.                
  3. realPosition.x = (realPosition.x - 0.5f);
  4. realPosition.y = (realPosition.y - 0.5f);
  5.                
  6. transform.position = realPosition;
  7.  

For some reason, this is not working properly, there is always a disconnect between the mouse/touch and the sprite's position.

Should I be doing something different? I feel I don't understand how NGUI's coordinates relate Unity's coordinates.

Thanks
- Ricardo

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite that follows mouse/touch position
« Reply #1 on: November 01, 2012, 01:03:42 PM »
Have you looked at UICursor?

ricardo

  • Guest
Re: Sprite that follows mouse/touch position
« Reply #2 on: November 01, 2012, 05:37:30 PM »
Have you looked at UICursor?

Where can I find it? I dont' see it in my NGUI folder.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sprite that follows mouse/touch position
« Reply #3 on: November 02, 2012, 02:37:34 AM »
It's a part of the inventory example that comes with a full version of NGUI.