I'm trying to make a sprite follow the mouse pointer. But when the sprite is sharing the position with the mouse it stutters.
I use the following script to get the direction the sprite need to move to get to the sprite:
extends Viewport
func get_dir(refrence : Vector2) -> Vector2:
return (get_mouse_position() - refrence).sign()
I use the following script to move the sprite:
var input_x = MouseControl.get_dir(get_position()).x
if(input_x != 0):
velocity.x = input_x * speed
velocity.x = clamp(velocity.x, -max_speed, max_speed)
else:
velocity.x = move_toward(velocity.x, 0, drag)
#...
# Some more code here which doesn't matter
#...
velocity.y += gravity
move_and_slide(velocity, Vector2.UP)
</pre>
And trying to add a threshold didn't help, it just added an offset to the sprite from the mouse.
<pre>extends Viewport
const threshold = 5
func get_dir(refrence : Vector2) -> Vector2:
var distance = get_mouse_position() - refrence
if abs(distance) < threshold:
distance = 0
return distance.sign()
Here's a video of the stutter: https://www.dropbox.com/s/5uly7v6xc4v12pv/5X8nWeZCDW.mp4?dl=0