How to make a CharacterBody2D (or Area2D) node detect what physics_layer you are colliding with on a tilemap?

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

Hello fellow Dev, I am having some trouble making a tile detector inside the player with Godot 4 that checks whenever it (the player) collides with a tile in a TileMap, it gets the physics_layer the tile is on and checks one of the collision_layer_value(s) if it’s enabled.

For example, I have a spike tile in a tilemap that is on a physics_layer that has a collision_layer_value of 3 that is enabled, and whenever the player touches the spike it prints “Spike detected, enabling project die.” and then it emits a function to kill the player.

If you are wondering what is with the “CharacterBody2D (or Area2D)” in the title, I was told by a more experienced Godot Dev that you don’t need an Area2D node in a Player Node and could just use the CharacterBody2D which is the main node of the Player node. But since I don’t know if that dev is lying or not I will give the option to use a Area2D node instead.

:bust_in_silhouette: Reply From: exuin

By definition, collisions can only happen ON the layers that match, so you should just match the layers that the player and the tiles have.

I would recommend using an Area2D, because CharacterBody2D has no signals when it collides with something.