Action upon contact with TileMap element

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

I’m writing simple game to learn Godot. In my game player can fire a bullet, when a bullet hits a wall it should destroy it.

I created my own TileMap, following GodotDocks. One of my “blocks” for building map is Wall.

My bullet is a RigidBody2D with Contact Monitor enabled and Contacts Reported set to positive number.
I connected body_entered(Object body) to my function.

My idea was to check if body is a wall, and if it is, run something like body.destroy() where destroy() is my function inside of Wall.

Problem is, body seems to be not instance of my Wall but instead it’s TileMap.
I’m not sure where to go from there. Is there a way to get exact cell being hit or should I solve this problem differently?

:bust_in_silhouette: Reply From: kidscancode

The whole point of a tilemap is that it batches all the tiles into a single collision object rather then hundreds of individual colliders. This is much more efficient, but it means that any collision with a tile is a collision with the TileMap object.

If you want to know what kind of tile you hit, you can get the collision point (this is available in RigidBody2D’s _integrate_forces() method) and pass that to world_to_map() to find the tile coordinate. Then you can use get_cellv() to find out what tile is at that location.