Unable to set RigidBody2D position

Godot Version

v4.4.stable.official [4c311cbee]

Question

In my game, the player is able to duplicate itself. The player nodes are RigidBodies and should be able to respawn if they fall off of the map. For some reason, duplicate players are unable to respawn. As soon as physics are enabled, they jump back to their old position.

Respawn code (delays added for testing):

func respawn(body):
	if not body == self: return
	if active:
		active = false # Enables freeze on rigidbody
		await get_tree().create_timer(2).timeout
		SceneFade.play_sfx("death.wav")
		Singleton.death_count += 1
		linear_velocity = Vector2.ZERO
		angular_velocity = 0
		ball_color = respawn_color
		refresh_stuff()
		set_deferred(&"position", respawn_location)
		await get_tree().create_timer(2).timeout
		active = true # Disables freeze, causes jump back to original position

Video of above code running:

I would rather not attach the project, but if it is necessary I can.

Update: There was a line of code that called queue_free() on the duplicate players’ camera nodes on ready. After removing this line, the issue was fixed. I have no idea why this fixed the issue, but it did. Does anyone know why?

Update: It still sometimes doesn’t work. I have given up on fixing the issue and instead I’m using annoying workarounds.

Are you doing this from _integrate_forces()? If not, try that. The docs do say you shouldn’t do what you’re doing to RigidBodies.

Another thing you could try is remove the body from the tree, wait a physics frame, and put it back?

Sounds a bit hacky, but.

I’m successfully freezing and moving around a RigidBody3D, I’d assume the 2D version works in the same way. That is to say, as far as I’m aware what you are trying should work.

You’ll probably need to show more code if you want people to help you further.

  1. How is active = false/true changing the freeze on the rigid body?
  2. How/where is the respawn code called?
  3. How do you duplicate the player?

I’m asking Q3, as I’m wondering if you aren’t duplicating everything correctly and something is getting shared causing the weird behaviour.

@gentlemanhal,

  1. freeze is set to true if active is false (set in physics_process)
  2. It’s connected to a signal in a weird, janky way that I wrote a long time ago
  3. I create a new instance of the player scene. The instance copies the necessary properties from the original player. (I can’t think of anything that these would share that would cause issues, but that’s the most likely situation that I can think of.)

I’ve attached a stripped version of the project below that shows the following issues:

  1. Sometimes the player respawns at the incorrect location
  2. Duplicate players cannot teleport by rolling over orange pads

I have made it so that when a player dies, all duplicates are deleted and recreated at the respawn location (this solves the original issue of this topic), but the original player’s position is still sometimes incorrect after respawning (usually off by 100-200 on the X axis).

Project ZIP file: https://www.mediafire.com/file/eadmq2rjz92qex4/balls_-_position_bug_2025-05-25_13-33-34.zip/file
Please note that a lot of the code in this project was written when I first started using Godot, so a lot of it sucks.