How to correctly update player's position by global mouse position?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jutoend

So, I’m updating players position with the following code, so I can limit it’s velocity.

position += (get_global_mouse_position() - position).normalized() * 30

But when the mouse is over the player, but, mininal or no movement are made at all, it starts shaking up and down as you can see in the gif bellow.

enter image description here

So, if someone could help me understand why this is happening and how to fix it, it would be very nice.

:bust_in_silhouette: Reply From: kidscancode

Your code is adding 30 pixels to the position in the direction of the mouse. But if the mouse is closer than 30 pixels, then you’re going to move 30, be past the mouse, and then move 30 back. You should only move if you’re more than 30 pixels away from the mouse, or even better, move a smaller distance when you’re closer to the mouse.