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.
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)