Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Make a gameobject follow the mouse when pressed held?

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

  1. Imags

    Imags

    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:
    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

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

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    587
    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