Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make a gameobject follow the mouse when pressed held?

Discussion in 'Scripting' started by Gjengaar, Aug 21, 2013.

  1. Gjengaar

    Gjengaar

    Joined:
    Feb 17, 2013
    Posts:
    6
    I need my gameObject to follow the mouse then I hold down the left mouse button.

    Similar to this; http://youtu.be/4PVGwmAotJM?t=16s

    When she touch the screen, the character moves towards the place the stylus in held down. :roll:

    So let say my gameObject is the character in the video, and when I hold down the mouse button the gameObject "walks" towards it.

    A little help here? :)

    Thanks. :)
     
  2. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    You could try to use something like http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html to get the 3D coordinates of the mouse cursor. The easiest way would be to use http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html and to move the character in that direction, but you would still run into problems with path finding assuming that you can have such problems. If not - this should probably work, but most probably isn't a great way to do it.

    I'm also not sure if this will work for mobile applications since I never touched them.
     
  3. Gjengaar

    Gjengaar

    Joined:
    Feb 17, 2013
    Posts:
    6
    Thanks, I will check it out. :)
     
  4. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    to add what @shadox said, if you are using 3rd party app for pathfinding (i'm sure you havn't done it yourself 'cos asking this kind of things) ,just put given value from mouseposition to target position ..or desired pos, whatever it is in system you using. Then again, if you dont have any pathfinding nor move system you might want to check: http://answers.unity3d.com/questions/60672/move-object-a-towards-object-b.html