destructible tilemap, destroying tiles through overlapcircle
hey guys im making a 2d and im 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[i].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");
}
}
}
Answer by melkior · Nov 08, 2021 at 04:14 PM
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);
2) 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.