![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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())