Area2D Mysteriously Triggering Anywhere

Godot Version

Godot 4.6

Question

Hello everyone,
Today I noticed my hidden easter-egg (a door with a funny popup upon interaction) could be interacted with anywhere in the scene. This isn’t supposed to happen, since an Area2D I put as a child of the door (StaticBody2D) with a script attached should’ve only let the popup show if the player is inside the Area2D. I checked the sizing of the Area2D, it’s parents and siblings but everything looked normal. Then I checked the player. Also perfectly normal. I am also sure there is nothing else controlling my interaction in the scene.

I have noticed, however, if I go up to the door and interact with how it should be normally done and try it again outside of the door, it does not trigger and functions as intended.

Any help? Thanks!

Without seeing your code that handles checking for the collision it’s hard to tell.

Here:

extends Area2D
var player_inside: bool = false

func _ready() -> void:
	body_entered.connect(func(b):
		if b.is_in_group("player"):
			player_inside = true
	)
	body_exited.connect(func(b):
		if b.is_in_group("player"):
			player_inside = false
	)

func _process(_delta: float) -> void:
	if player_inside and Input.is_action_just_pressed("interact"):
		print("interacted")

I suppose the first few things I would do is:
Put a print statement in both the body entered and body exited functions to see when and how they trigger.
Maybe print the state of player_inside every frame or so as well.

1 Like

I’ve already tried that- the moment my player joins it prints the player is inside the Area2D, when it’s not.

The only thing I can think of is maybe you accidentally put another object in the “player” group which is in the same position as the door when your game starts.

1 Like

Well, I put the parent of the Area2D (StaticBody2D) in the group, because otherwise the popup would not be triggered at all.

Then there’s your issue.

1 Like

I wouldn’t even use a group for this, I would instead check the player based on their class. I assume your player has their own script, such as “player” with a class_name set? Just check for that, instead of checking for the group.

Ok, I added a class_name to my player script (main.gd, like this: class_name Player) and changed my body_entered and body_exited functions to this:

body_entered.connect(func(b):
		if b is Player:
			player_inside = true
	)

but now nothing happens.

Are your collision masks set up correctly?

Definitely. I’ve checked them many times.

Next I would test here:

body_entered.connect(func(b):
		if b is Player:
			player_inside = true
            print("player entered zone")
	)

and here:

func _process(_delta: float) -> void:
    if player_inside:
        print("player is inside in process")
	if player_inside and Input.is_action_just_pressed("interact"):
		print("interacted")

And if we could see your scene tree please?

Nothing printed, and here’s the tree:

What’s the warning say at the Door object? Do you have a collisionShape attached to it?

Ah, that door is for something unrelated (switching scenes). FakeDoor is the one I have a problem with. I do not have a CollisionShape attached to it, though.

I noticed that your Area2D for that door is inside a staticbody2D, that might cause issues. Could you try and move it outside of the staticbody2D and see if that changes anything?

2 Likes

I moved it outside and it worked! Ty.

1 Like