How to erase a tile based on collision

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

I am trying to erase a tile when a collision happens, the following is part of a projectile class that is a CharacterBody2D. I want to erase a tile when it collides with a tile in Tilesets/Breakable.

My detection code works, but I am not mapping something correctly with the collided tile to erase it.

    var breakable_map
    var collision
    var collided = false
    	
    func _ready():
    	breakable_map = get_parent().get_node("Tilesets/Breakable")
    	pass
    	
    func _physics_process(delta):
    	if !collided:
    		calc_velocity.x = SPEED * delta * direction
    		translate(calc_velocity)
    		collision = move_and_collide(calc_velocity )
    		$AnimatedSprite2D.play("shooting")
    	
    	if collision != null and !collided:
    		collided = true
    		
    		var body = collision.get_collider()
    		
    		if body.is_in_group("enemies"):
    			body.hit(HIT_POINTS)
    		elif body.name == "Breakable":
    			var tile = breakable_map.local_to_map(breakable_map.to_local(to_global(position)))
    			breakable_map.erase_cell(0, tile)
			
:bust_in_silhouette: Reply From: Moreus

there’s can be small differences in GD 4.0

Moreus | 2023-04-04 16:03

Thanks, I am still adjusting to version 4!

keidav | 2023-04-05 02:34

:bust_in_silhouette: Reply From: blobmcbarnum

Hi!
I had the same issue, and for Godot 4 I used local_to_map:

var collider = get_collider()
	if collider is TileMap:
		var colliderPosition = get_collision_point()
		var cellPosition: Vector2 = collider.local_to_map(colliderPosition)