Author Topic: Make an object always follow your mouse  (Read 2036 times)

matt1

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 28
    • View Profile
Make an object always follow your mouse
« on: November 29, 2014, 09:50:44 AM »
Hi,


I am trying to make a GUI object always follow my mouse, I've tried to alter the DraggableObject script to always be true but so far havn't prevailed, is there an easier way to do it?

Many thanks

Matt

matt1

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Make an object always follow your mouse
« Reply #1 on: November 29, 2014, 10:41:31 AM »
I managed to achieve this by doing:

  1. public class DragGUI : MonoBehaviour {
  2.  
  3.         public GameObject DraggableSprite;
  4.         public Camera NGUICam;
  5.  
  6.         public Vector3 MoveUI(Vector3 Pos, Transform localSpace, Camera cameraNGUI) {
  7.                 Pos.x = Mathf.Clamp01 (Pos.x / Screen.width);
  8.                 Pos.y = Mathf.Clamp01 (Pos.y / Screen.height);
  9.  
  10.                 Vector3 WorldPos = cameraNGUI.ViewportToWorldPoint (Pos);
  11.  
  12.                 return localSpace.InverseTransformPoint (WorldPos);
  13.         }
  14.  
  15.  
  16.         void Update() {
  17.                 DraggableSprite.transform.localPosition = MoveUI( Input.mousePosition, DraggableSprite.transform.parent, NGUICam);
  18.         }
  19. }

matt1

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Make an object always follow your mouse
« Reply #2 on: November 29, 2014, 12:26:29 PM »
A problem i've ran into, that I can't figure a way around is:

If I have MouseButton(0) or (1) held down then void OnHover(bool IsOver) will not work, how can I work around this?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Make an object always follow your mouse
« Reply #3 on: November 30, 2014, 05:50:04 AM »
When the mouse button is held down, you get OnDragOver/OnDragOut instead of OnHover.