0 votes

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 onbodyentered / onbodyexited 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("onbodyentered", mainscript, "onfridgeentered")

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())
Godot version 3.2.3
in Engine by (12 points)

1 Answer

0 votes

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 "bodyentered(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.

by (87 points)

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

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.