Destroy a specific tile using area 2d

Godot Version

` 4.3

Question

I have a major scene that has 2 nodes: tilemaplayer and player (kinematicbody with area 2d and sprite).
My objective was to make player’s Area2D destroy the tile and God only knows how many things I’ve trid, below is my code but honestly it passed through various variations. This in specific is to know if it is getting the atlas coords (it doesn’t btw, returns to -1,-1 meaning its null. However player is affected by tile collision)

func _on_pickaxe_body_entered(body: Node2D) → void:
if body is TileMapLayer:
tmp = body
var s = tmp.get_cell_atlas_coords(tmp.position)
#var tile_id = tmp.get_cell(tile_pos.x, tile_pos.y)
print(s)

Please help I’m in this rabbit hole for 2 days

How does the area look like? and is this game topdown?

It’s a platformer. The area 2d is supposed to work like a pickaxe in these blocks
This red square is the collision shape for area 2d

The problem is that “body” is the whole tilemap, so you try to get the cell at the tilemaps position (which is probably (0,0)).

My suggestion would be to use a raycast2d-node instead of the area (or a shapecast2d-node)

1 Like

Feel like I’m almost there but dont know why it stil returns to -1,-1

func _input(event: InputEvent) → void:
var rc = $RayCast2D
if Input.is_action_just_pressed(“mine”):
if rc.is_colliding():
var tmp = rc.get_collider()
var l = rc.get_collision_point()
var point = tmp.get_cell_atlas_coords(l)
print(point)
else:
print(“nothing”)

I used print to test every var and the problems begin the var point where it returns to -1,-1.
I also made variations to this code

if Input.is_action_just_pressed(“mine”):
if rc.is_colliding():
var tmp = rc.get_collider()
var l = rc.get_collision_point()
var cell = tmp.local_to_map(l)
var data = tmp.get_cell_atlas_coords(cell)
print(data)
else:
print(“nothing”)

This one is interesting because it’s last function always returns to null but cell works pretty well. At the image below I’m printing cell, not data, just to show I’m convinced it shows right position of the tiles.

the problem might be, that the collision_point is right at the edge of the cell, so you might have to add +1/-1 to make sure its inside the cell

doesn’t work
don t know what to do anymore
but thx for trying :slight_smile: