How can I tell a KinematicBody2D to move_and_collide() until it reaches a certain vectoral position?

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

I’m still getting acquainted with how physics and such are handled in Godot and I couldn’t find a way to move_and_collide() and stop when the KinematicBody2D reaches the position I want it to be at. For example, I want my body to move_and_collide(Vector2 (5, 0)) until position == Vector2 (50, 0) is true. This seems like it could be done with a simple if statement or while loop, yet I think there is an odd connection between move_and_collide() and the position of the body. How is it even calculated and how can I accomplish what I want?

:bust_in_silhouette: Reply From: exuin

When move_and_collide() is called it moves the body for the frame that it’s called on. If you put it in a while loop then it will just move the body in that single frame until its at its destination, which probably isn’t what you want. Instead, use an if statement to check if the body is at its destination yet and only move if it isn’t.