if you want to detect what tile in a TileMap a RigidBody2D collides with, you can use the get_colliding_bodies() method in your RigidBody2D script. This returns a list of all RigidBody2D nodes that are currently colliding with the body.
Once you have this list, you can iterate over it and check if the collision is with a TileMap node by using the is_in_group() method and passing in the "tile_map" group.
Next, you can get the position of the collision point in tile coordinates by using the map_to_world() method on the TileMap node and then the world_to_map() method to convert from world to tile coordinates.
Finally, you can change the tile at the colliding position by using the set_cell() method on the TileMap node.
Gdscript;
func _on_RigidBody2D_body_entered(body):
if body.is_in_group("tile_map"):
# Get the TileMap node
tile_map = body.get_node("/root/TileMap")
# Get the collision point in world coordinates
collision_point = body.get_collision_point(0)
# Convert collision point to tile coordinates
tile_coordinates = tile_map.world_to_map(collision_point)
# Change the tile
tile_map.set_cell(tile_coordinates.x, tile_coordinates.y, 0)
Ofcourse you have to go to the tiles and in ready() write;
func ready():
add_to_group("tile_map")
alternatively you can use "get_collision_mask" function of the TileMap node. This function returns a collision mask that can be used to find the specific tile that the rigidbody collided with.
this is gonna be attached to the rigidbody:
func _on_Area2D_body_entered(body):
if body.get_parent().get_parent().get_name() == "TileMap":
tile_map = body.get_parent().get_parent()
mask = tile_map.get_collision_mask(body.get_position())
# Check if the mask corresponds to a valid tile index
if mask > 0:
# Get the tile coordinates in the tile map
tile_x, tile_y = tile_map.world_to_map(body.get_position())
# Change the tile or perform other actions
# ...
Note that in this example, the assumption is made that the TileMap node is a direct parent of the Area2D node attached to the rigidbody. You may need to modify the code to match the specific hierarchy of your scene.
Hope this helps