How to move object to mouse position smoothly?

  • I'm trying to get an object to mouse position on one axis, instantly and smoothly. I want to set the velocity instead of setting the position to the mouse position since I want to check FlxG.collide() on the object. But it keeps "shaking" around the mouse.
    I make the object move to the mouse by:

    • Calculate the distance to mouse (negative means up or left, position means down or right)
    • Store the distance sign (negative or position)
    • Set the object velocity according to the distance sign
    • In update(), if the distance has changed its sign then stop the object

  • I'm thinking something like

    if(velocity.x > 0 && x >= mouse.x)
    velocity.x = 0;
    x = mouse.x;
    if(velocity.y > 0 && y >= mouse.y)
    velocity.y = 0;
    y = mouse.y;

    then something similar for if velocity.x/y < 0, etc. etc.

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.