+3 votes

Hi there!

I'm trying to set the position (to restore all elements back to their original position when the player dies) of a RigidBody2D with RigidBody2D::set_pos but unfortunately, it don't doesn't change the position.
Is there any other way than set_pos()?

Best regards,

in Engine by (709 points)
edited by

(Not an answer, since its a workaround), but if you make the level a scene, and when the player dies, delete and reinstance the level node, you won't have to return everything to original values.

BTW, are you using _process to update the positions?

I am using a save-anywhere mechanism, so I'm saving position and velocity of every object when saving the game. And no, I'm using a function which is called at the and of an animation played in an AnimationPlayer.

And btw doesn't reinstancing the scene take loading time?

3 Answers

+3 votes
Best answer

Please make sure to always call set_pos and set_global_pos inside a fixed process for physics objects, even when resetting them. See the docs.

This is a very common pitfall when dealing with Godot physics. It tricked me more than once.


var should_die = false

func _ready():

func reset():
    should_reset = false

func _fixed_process(delta):
    if should_reset:
        # CORRECT
        # Will always work

        # WRONG
        # Might not work
        # same goes if called from _process or input signal/_input callback

EDIT: The same apply for set_linear_velocity set_angular_velocity. Setter and getters alike (you might get incorrect values from getters outside of the fixed process).

by (98 points)
selected by
0 votes

try set_global_pos()

by (852 points)
+10 votes

When I try to set the position or rotation of a Rigidbody2D in the _physics_process function in Godot 3.0, the body immediately jumps back to its previous position.

It only works in the _integrate_forces function where I check if a reset variable was set to true and then I reset the body in this way:

func _integrate_forces(state):
    if reset:
        state.transform = Transform2D(start_angle, start_pos)
        state.linear_velocity = Vector2()
        reset = false
by (74 points)

How do i move the rigidbody to a position2d using this?

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.