Two RigidBody3Ds constantly triggering Area3D

Version 4.6.2

Question

Hi, currently I am just trying to debug a problem I am having with a bigger project in a smaller one. When I put a RigidBody3D inside of an Area3D (which is triggered when the area is entered) and then reparent it to the Area3D, I have managed to avoid it constantly retriggering the body_exited signal (which would reparent it to the Area3D’s parent.)

However, as soon as a second RigidBody3D enters the Area3D, it starts constantly triggering the body_entered and body_exited signal because it is constantly reparenting not only the second RigidBody3D, but also the first!

If anyone could help me with this problem it would be great. Thank you very much in advance.

Below is the code for the body_entered and body_exited functions.

extends Area3D


@onready var parent : Node3D = $".."


func _on_body_entered(body: Node3D) -> void:
	if body.get_parent() == self:
		return
	# Add the body as a child of the area3d.
	print("body entered: ", body)
	# Disable monitoring while the body is being reparented (to avoid false positives
	# while the body exits the scene tree.)
	set_deferred("monitoring", false)
	# Reparent the body to be a part of the area.
	body.reparent.call_deferred(self, true)
	# Enable monitoring again.
	set_deferred("monitoring", true)


func _on_body_exited(body: Node3D) -> void:
	# If a body has a parent that is not the Area3D, it won't need reparenting.
	if body.get_parent() != self:
		return
	# Add the body as a child of the area3d's parent.
	print("body exited: ", body)
	body.reparent.call_deferred(parent, true)

Can you explain a little more the context of this? What are you trying to achieve in your project with this reparenting?

Hi, thanks for the response! I am basically trying to get ingredients (RigidBody3Ds) to be able to be put on a plate and then carried around. I felt the best way to do this is to have a RigidBody3D as the plate and then have an Area3D attached to it that will add any ingredient that enters it be reparented to the plate.

The reason I don’t just put them on top and rely on physics is because the plate’s position is sometimes directly changed, and they’d stop being on top of the plate if they weren’t children of it.

Is this plate going to be stationary or moving?
Do you want your ingredients to still be affected by physics, or be stationary once attached to the plate?

The plate will be moving attached to the player (CharacterBody3D.) The ingredients are always affected by physics but I’ve managed to stop them falling out of the plate using CollisionShape3Ds.

If you want the ingredients to be affected by physics, then what’s the point of reparenting them to the plate? Let them be parented to the world and be affected by the general world physics.
By reparenting it to a moving body, you’re messing with the RigidBody3D’s physics, because it will try to copy the transform of its moving parent and be affected by the external physics at the same time.

That’s a good point, I’d mostly wanted to reparent it to save time on having to manipulate the physics to simulate the plate being “picked up“ by the player. But I see that it might be simpler overall to just get that working. Thank you for the help!

1 Like