Search code examples
godotgodot4

Godot body_entered only for a specific object in the scene


In my project, I have an Area2D. I want to perform some actions if a CharacterBody2D I created overlaps it.

I am thinking of using the body_entered signal to do this. I connected the signal, but how do I determine which character body triggered the signal? I have multiple character bodys in the scene and only one can trigger this action on overlap.


Solution

  • Best way imo, is to use layers and masks.

    If you have a specific element like a player give it it's own layer. Then in your case set the collision mask to the specific layer.

    Your area will then only collide with elements on the specified layer. No code needed at all.