0 votes

Hey, I'm having trouble moving an object (a RigidBody2D) using the Position variable. When I run the code, the position of the object changes for a moment, but then returns to its previous position.

The object is a scene instantiated in another one, a RigidBody2D (Character), which has as children a CollisionShape2D and a Sprite. I have tried with a similar object and everything goes well, but this object appears in the indicated position for a moment, and then it returns to its original position. There is no code that is causing this.

Godot version Godot Engine v3.4.4
in Engine by (12 points)

1 Answer

+2 votes

That's because, as indicated in the docs, you cannot change a rigid body's position directly.

A rigid body is controlled by the physics engine, which calculates the forces on it, any collisions, and determines its velocity and position. This is the point of using a rigid body, so that you don't have to do all those calculations yourself.

If you're trying to control its movement, then you have to apply forces.

If you want to do a one-time change, like a teleport, then you can do that by directly altering the physics state in _integrate_forces().

For more information, see the RigidBody2D docs at:
https://docs.godotengine.org/en/stable/classes/class_rigidbody2d.html

by (21,716 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 Frequently asked questions and 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 [email protected] with your username.