Trigger single Area2D out of multiple at the same location

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

Hello everyone.

In my game there is a finish area. The object of the game is to collect all the keys in the finish area. When ball enters the finish area only one key should be collected.

I have made a Key scene with Area2D as root node & added a sprite & CollisionShape2D to it. I place several (up to 3 or 4) keys (scenes) in the finish area with different rotations (positions will be slightly different). As the finish area is not so big the keys will overlap each other.

When a player enters the area, only one key should be collected, how can I achieve that? Thanks!

Note: when a key is collected, I remove the key as well as that player from the scene.

:bust_in_silhouette: Reply From: vbdev

Hello, I solved the issue using following logic:

The keys are already in a group “Keys”. So, in body_entered() event I checked number of nodes in the group & if more than 0 then removed first item in the array - get_nodes-in_group(“Keys”)