Stopping a player node when destination is reached

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


I have a question about using a direction vector and ensuring that a player reaches a specific target position using the mouse. I know that I can calculate the direction vector by subtracting the source from the destination position and then use this Vector in my move_and_slide function to reach a specific point based on mouse position, similar as depicted here:

Now, what I try to do is let the player move to the mouse position based on a mouse click. I am able to get to the point of the mouse position but unlike the example of above link, the player does not stop and just walks over that position. I know that I can simply use an if condition to check whether the character has reached his position, but I was wondering if there is a more elegant solution to this? I thought that the move_and_slide function would cater for only moving to that specific position if I pass in the respective Vector position. What other options would there be to ensure that the character stops at exactly the point where the mouse has clicked?

:bust_in_silhouette: Reply From: estebanmolca

You can use linear_interpolate():

Vector2 linear_interpolate ( Vector2 b, float t ) Returns the result
of the linear interpolation between this vector and b by amount t. t
is in the range of 0.0 - 1.0, representing the amount of


Thanks for your answer! It helped me to understand this correctly!
So this works, however, your solution does speed up and slows down at the end,
but I needed a static speed. So instead of using linear_interpolate, I went
for move_toward which I tried to use before, but I guess I misunderstood
its use. So now this works fine, thanks again!

BennieBe | 2020-05-30 21:21