How do you detect a rigidbody entering an area 2d? On_body_signal not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By seatoad

Hi, I am trying to detect a rigid body entering / leaving an area 2d.
I have an egg (rigid body 2d, with collisionshape2d) object and a fridge (Area 2D with collision shape 2d as a child) object.

I am new to godot so I just have one big script in the root node, which is a node2d, which has all the signals connected to the script. I want to detect if the egg is currently in the fridge collision box, because this is a drag and click game. I want it so that if the eggs are entered in the fridge’s collision box, it updates a boolean that tells the egg to either appear or dissappear. For example, if the eggs are in the fridge when the mouse exits, then the eggs should dissappear, but if the eggs are not in the fridge when the mouse exits, it means the eggs are out of the fridge so it should appear. But for some reason the on_body_entered / on_body_exited signal doesn’t work :(.

I also tried to use the connect() command but I’m not sure if I’m not using it right but basically in my egg script I referenced the parent node with the script and did:
connect(“on_body_entered”, mainscript, “on_fridge_entered”)

Any help is appreciated. I also changed the monitor collisions to true, and changed the number to 1.

extends Node2D


# Fridge Variables
var fridgeanim = [preload("res://assets/cookinglevel/fridge_closed.png"), preload("res://assets/cookinglevel/fridge_opened.png")]
onready var fridge = get_node("fridge")

# Egg Variables
onready var eggs = get_node("eggs")
var eggsinfridge = true

func _ready():
	eggs.visible = false
	eggs.z_index = 2

func _on_fridge_mouse_entered():
	fridge.z_index = 1
	fridge.get_node("fridge/door").set_texture(fridgeanim[1])
	eggs.visible = true


func _on_fridge_mouse_exited():
	fridge.z_index=0
	fridge.get_node("fridge/door").set_texture(fridgeanim[0])
	if (eggsinfridge == false):
		eggs.visible = true
	elif (eggsinfridge == true):
		eggs.visible = false

func _on_fridge_body_entered(body):
	if (body.get_name() == "eggs"):
		eggsinfridge = true
	else:
		print(body.get_name())

func _on_fridge_body_exited(body):
	if (body.get_name() == "eggs"):
		eggsinfridge=false
	else:
		print(body.get_name())
:bust_in_silhouette: Reply From: aadyainfotainment

The Area2d should be a child of the node 2d.
It should have a collision shape.

the rigid body should have a collision shape.
Ensure your rigid body and area 2d are on the same collision layer.

In your area2d connect the signal “body_entered(body: Node)” to the Node2d(which is having the script) . This will work when body enters area 2d.
To check body exited connect the signal “body_exited(body: Node)” to the Node2d.

Important: (The Monitoring and Monitorable properties of the Area2d should be on)

Hope this helps.

Also it has to be on the correct layers to be detected

Wakatta | 2021-12-19 11:39

1 Like