Make a gameobject follow the mouse when pressed held?

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

  1. Imags

    Imags

    New Member

    Joined:
    Feb 17, 2013
    Messages:
    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

    Member

    Joined:
    Aug 25, 2010
    Messages:
    255
    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. Imags

    Imags

    New Member

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

    chelnok

    Member

    Joined:
    Jul 2, 2012
    Messages:
    403
    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