Rigidbody2D repositioning after print()

Godot Version

4.2.2

Question

Hey, so I’m making a game where you are a crab and have to grab shells and throw them at other crabs. The crab has a Area2D node for its hand. Here is the code:

extends Area2D

var hand_is_free = true
var object

func _on_body_entered(body):
	if body.is_in_group("Object"):
		object = body

func _physics_process(delta):
	if Input.is_action_just_pressed("grab") && hand_is_free:
			object.reparent(%Hand, true)
			object.global_position = global_position
			hand_is_free = false
			
	if not hand_is_free:
		if Input.is_action_just_pressed("throw"):
			object.apply_central_impulse(Vector2(0, 200))
			hand_is_free = true

When I applied the central impulse to the object, it would return to its original global_position and only then apply the force, like I hadn’t reparented it. I really didn’t know what was wrong, since I wanted the object to impulse out of the crab’s hand, and spent at least half an hour to fix it.

It turns out, when I tried printing the object’s position (print(object.global_position)), it just magically worked. Literally, this is the only thing I changed and it worked. When I tried erasing this line, it stopped working again.

This isn’t really a question, just wanting to share this situation here. Idk if it is a Godot’s bug or what, but I feel like I am not the first person with this issue.

Maybe a godot bug! Rigid bodies are not supposed to be moved with position/global_position, instead of reparenting try attaching the shell with a PinJoint2D

2 Likes

Thank you! I am testing Godot after learning Unity and had no idea of the joints. It really helped, thanks!