0 votes

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.

in Engine by (35 points)

1 Answer

+1 vote
Best answer

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.

by (19,911 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.