Unity Community


Results 1 to 9 of 9

Thread: Mouse Follow


  1. Posts
    11

    Mouse Follow

    Hello!

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

    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. Location
    Canada
    Posts
    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....



    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. Posts
    11
    Would you mind posting a full example? I'm not sure how to set up transform.Forward properly.

    Cheers!

  4. Wox Wox is offline

    Location
    Sweden
    Posts
    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
    "Everything should be made as simple as possible, but not simpler."


  5. Posts
    11
    Thanks (bockar och bugar)! I'll take a look at it!


  6. Posts
    11
    That worked pretty well. How do I change the speed of the object? It's moving quite fast.


  7. Location
    Deep in the jungle where almost no internet is
    Posts
    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
    god i have to learn a lot more 0.0


  8. Posts
    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. Posts
    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:  
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •