Destructible tilemap, destroying tiles through overlapcircle

Hey guys, I’m making a 2D and facing a problem.

I currently have a Tilemap and i would like to make it so that the projectile can destroy tiles within a certain radius (through overlapcircle) i have created an collider2d that detects stuff and it does detect the tilemap object however i’m not really sure how i would destroy the tiles within it… the collider returns a tilemap is there anyway i can access which tiles the projectile has collided with thru the tilemap gameobject i get?

My code for detecting collision:

IEnumerator OnCollisionEnter2D(Collision2D x)
{
    if (x.gameObject.tag != "Player")
    { 
        Collider2D[] collidedwith = Physics2D.OverlapCircleAll(this.gameObject.transform.position, radius);
        for(int i = 0; i<collidedwith.Length; i++)
        {
            if (collidedwith*.tag == "Tilemap") {} //I would like to destroy the tiles here
        }
        yield return new WaitForSeconds(2);
        if (this.gameObject != null)
        {
            this.gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
            this.gameObject.GetComponent<Rigidbody2D>().angularVelocity = 0;
            Destroy(this.gameObject);
            GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().GetComponent<Follow>().toChase = GameObject.Find("Player");
        }
    }
}

There’s probably half a dozen approaches but here is two:

  1. Define a ‘destroyed’ sprite. (some rubble, blackened earth, or just the ground) and then replace the sprite that was there with the new destroyed sprite within the tilemap
myTileMap.SetTile( v3Int , newSprite );
  1. Alternatively if you wish to remove the sprite you can null it out
myTileMap.SetTile( v3Int , null );

Again - probably other things you can do.

Remember you are designing a feature that makes people thing something has been destroyed.

Impressionism is core to game design - give them the impression its destroyed and they will think that. You don’t literally have to destroy it.

Unfortunately I am not aware of a way to detect exactly which tile you hit. I know Im a little late but I wanted to post my solution for anyone else looking for it that needs it. What I did was loop through all tiles within a box with sides of the radius and then check distance to make it a circle.

If that sounds confusing then heres my code. A little messy but it gets the job done:

Vector2 pos = transform.position; //center of the circle
ground = FindAnyObjectByType<Tilemap>();//Get the tilemap
for (int x = -ExplosionRadius; x < ExplosionRadius; x++)
for (int y = -ExplosionRadius; y < ExplosionRadius; y++) //find the box
{
    Vector3Int Tilepos = ground.WorldToCell(new Vector2(pos.x + x, pos.y + y));
    if (Vector3.Distance(pos, Tilepos) <= ExplosionRadius) //check distance to make it a circle
    {
        ground.SetTile(Tilepos, null);
    }
}

This worked fine for me. Hope this helps anyone.