+1 vote

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

Godot version 3.3.3
in Engine by (13 points)

2 Answers

0 votes

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 )

by (2,560 points)
0 votes

Target.gd

extends Position2D
signal follower_summoned(position)

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

Follower.gd

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.

by (846 points)
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.