0 votes

I'm still getting acquainted with how physics and such are handled in Godot and I couldn't find a way to moveandcollide() and stop when the KinematicBody2D reaches the position I want it to be at. For example, I want my body to moveandcollide(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 moveandcollide() and the position of the body. How is it even calculated and how can I accomplish what I want?

in Engine by (20 points)

1 Answer

+1 vote

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.

by (7,338 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.