Mouse Follow

Discussion in 'Scripting' started by StickyNavels, Dec 13, 2009.

  1. StickyNavels

    StickyNavels

    New Member

    Joined:
    Dec 13, 2009
    Messages:
    11
    Hello!

    Decided to give Unity a go - and I've really enjoyed it so far. Now, I'm stuck. :oops:

    I'm trying to make an object follow the mouse cursor when the left mouse button is pressed down (this is in 2D space). I've dug up a few threads on this topic, but I still can't quite puzzle it together. I'd also like to add acceleration and retardation, as well as inertia, to the object in question.

    I've taken a gander at the script reference, but me being totally green it's not making too much sense.

    I'd really appreciate someone shedding some light on this. :)
  2. DavidB

    DavidB

    Member

    Joined:
    Dec 13, 2009
    Messages:
    530
    I'm by no means a pro...but when I was rigging up a small tech demo of what Unity can do... I managed to rig up a flying spaceship that would orient itself towards where I clicked on the screen in 3d space.

    This was the key line....

    Code (csharp):

    Essentially this made the ship "look" at the point in space where I clicked. That way in the update method I could use the transform.forward direction to move.

    The ScreenPointToRay method was the key to accomplishing this as it can take where you click and convert it to a world space co-ordinate. (the get point was just so I could make it a farther distance so that the difference between my camera (which would orbit around the object) and the ship would not be very noticable. I don't know if it was required to be that long..it was just a quick and dirty test.

    Hope this somewhat helps.

    Cheers
  3. StickyNavels

    StickyNavels

    New Member

    Joined:
    Dec 13, 2009
    Messages:
    11
    Would you mind posting a full example? I'm not sure how to set up transform.Forward properly.

    Cheers!
  4. Wox

    Wox

    New Member

    Joined:
    Dec 14, 2009
    Messages:
    86
    Hi my first post on this forum. Thanks to all people that giving great feed back and helping out.


    Maybe this Followmouse2d is to some help.

    Attached Files:

  5. StickyNavels

    StickyNavels

    New Member

    Joined:
    Dec 13, 2009
    Messages:
    11
    Thanks (bockar och bugar)! I'll take a look at it!
  6. StickyNavels

    StickyNavels

    New Member

    Joined:
    Dec 13, 2009
    Messages:
    11
    That worked pretty well. How do I change the speed of the object? It's moving quite fast.
  7. nalim

    nalim

    New Member

    Joined:
    Apr 13, 2010
    Messages:
    118
    Nice i use it too thanks
    but i was thinking how to let it follow everytime
    so you dont have to click before it cdomes to there
  8. Cyttil Dalionzo

    Cyttil Dalionzo

    New Member

    Joined:
    Jan 3, 2011
    Messages:
    2
    Hey Wox,
    very nice script...
    I've been trying to make this work with raycasting for like 1/2 hours, but you solved it for me!!!
  9. Cyttil Dalionzo

    Cyttil Dalionzo

    New Member

    Joined:
    Jan 3, 2011
    Messages:
    2
    @StickyNavels,

    At the moment, the script doesn't have any speed, it just goes right to the spot you clicked on...
    I'm not a pro, but I think you could implement speed by getting the distance between your player and the spot,
    normalize that distance (distance.normalized) and make the player Translate along that

    e.g.
    -----
    Code (csharp):
    1. var hit_x = hit.point.x;
    2. var hit_z = hit.point.z;
    3. moveDirection = Vector3(hit_x, 0, hit_z) - transform.position;
    4.  
    -------

    I hope that helps you out,
    Dalionzo