A way to approach a vector2 in a constant speed

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

I’m in a scenario where I want a vector to approach the x and y values of a second vector in a set amount of speed, I’ve tried using move_toward() and linear_interpolate() but both of them slow down more and more the closer it gets to the second vector

:bust_in_silhouette: Reply From: Inces

move_toward should work, perhaps You made a math mistake in code ?

But it looks like a job for Tween node. It will calculate speed and interpolation, by taking only time and values.

If You are talking about position vectors, than You can just subtract one from another, get the lenght of result for distance, normalize the result for direction, and calculate constant velocity by yourself. ( result lenght divided by time )

:bust_in_silhouette: Reply From: yrtv


extends Position2D
signal follower_summoned(position)

func _physics_process(delta: float) -> void:
	if Input.is_action_pressed("ui_accept"):
		emit_signal("follower_summoned", position)


extends Sprite

export var speed:int
var target_position:Vector2 = position

func _physics_process(delta: float) -> void:
	if position != target_position:
		position = position.move_toward(target_position, delta * speed)

func _on_Target_follower_summoned(target_position_recived:Vector2) -> void:
	target_position = target_position_recived

Connect follower_summoned signal of Target to _on_Target_follower_summoned function of Follower. Set speed variable in editor. Run and press Spacebar.