The problem is that your code doesn’t consider the fact that your character might (and likely will!) move past the target. You correctly calculate velocity
as the product of character_speed
and direction
. Here, the character_speed
determines the length of the velocity
vector, so you need to cap it at precisely the point where your character would overshoot the target. It’s not a problem if it’s smaller (i.e. the character moves towards the target, but won’t reach it in the next frame), but it cannot be bigger! Calculate the distance to the target_position
and divide it by delta
to determine the maximum speed your character could move with the next frame without shooting past the target. Then simply take the minimum of your usual character_speed
and that max_speed
:
func _physics_process(delta: float) -> void:
var direction = global_position.direction_to(target_position)
var distance = global_position.distance_to(target_position)
var max_speed = (distance / delta)
velocity = min(character_speed, max_speed) * direction
move_and_slide()