Detecting Collision with Specific TileMapLayer and Deleting the Cell

4.4

How do I detect collision with a TileMapLayer node and then delete the cell?

I know enemy could be a Static Body but I am specifically trying to learn collision with a TileMapLayer node

extends CharacterBody2D

@export var speed = 50
var direction = 1
var right_boundary = 288
var left_boundary = 17
const GRAVITY = 200.0

@onready var tile_map_layer = $"../Enemy"  

func _ready():
	position.x = left_boundary

func _physics_process(delta: float) -> void:
	# Apply gravity
	velocity.y += delta * GRAVITY
	# Calculate movement
	var motion = velocity * delta
	move_and_slide()

	velocity.x = speed
	# Play animation
	if velocity.x != 0:
		$AnimatedSprite2D.play("run")
	else:
		$AnimatedSprite2D.play("idle")

func turn_around():
	direction *= -1  
	$AnimatedSprite2D.flip_h = direction < 0  


func _on_enemy_detector_area_2d_area_entered(area: Area2D) -> void:
	var tilemap = $"../Enemy" 
	var tile_pos = tilemap.local_to_map(area.global_position) 
	if tilemap.get_cell_source_id(0, tile_pos) != -1: 
		tilemap.erase_cell(0, tile_pos) 
		print("Enemy tile removed at: ", tile_pos)

You have two issues with your current code.

  1. Your _on_enemy_detector_area_2d_area_entered function is trying to detect an Area2D entering another Area2D, but TileMap nodes don’t automatically emit area signals.

  2. You’re using local_to_map() on the area’s global position, which might not give accurate results.

Here is a code block I wrote. I tested it and it “Worked”. I wouldnt use this in my own game. But more power to ya for learning.

extends CharacterBody2D

@export var speed = 50
var direction = 1
var right_boundary = 288
var left_boundary = 17
const GRAVITY = 200.0

@onready var enemy_tilemap = $"../Enemy"  

func _ready():
    position.x = left_boundary

func _physics_process(delta: float) -> void:
    # Apply gravity
    velocity.y += delta * GRAVITY
    
    # Set horizontal velocity
    velocity.x = speed * direction
    
    # Move character
    move_and_slide()
    
    # Check for collision with tiles
    check_tile_collision()
    
    # Play animation
    if velocity.x != 0:
        $AnimatedSprite2D.play("run")
    else:
        $AnimatedSprite2D.play("idle")

func turn_around():
    direction *= -1  
    $AnimatedSprite2D.flip_h = direction < 0  

func check_tile_collision():
    # Get collision info
    for i in get_slide_collision_count():
        var collision = get_slide_collision(i)
        var collider = collision.get_collider()
        
        # Check if we collided with the enemy tilemap
        if collider == enemy_tilemap:
            # Convert collision point to tile coordinates
            var tile_pos = enemy_tilemap.local_to_map(collision.get_position())
            
            # Check if there's a tile at this position
            if enemy_tilemap.get_cell_source_id(0, tile_pos) != -1:
                # Erase the tile
                enemy_tilemap.erase_cell(0, tile_pos)
                print("Enemy tile removed at: ", tile_pos)