- Home /
Destroy Tiles That Collide With Object
Hello! Sorry to ask a question that some others have asked but I have not seemed to be able to find a solution. Here's what I have so far:
 using System.Collections;
 using System.Collections.Generic;
 using System.Numerics;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class DestroyTilesOnCollision : MonoBehaviour
 {
 
     public Tilemap tilemap;
     public ContactPoint2D[] contacts;
 
     void OnTriggerEnter2D(Collider2D collision)
 
     {
 
         if (collision.gameObject == tilemap)
 
         {
 
             collision.GetContacts(contacts);
             UnityEngine.Vector3 hitPosition = UnityEngine.Vector3.zero;
             foreach (ContactPoint2D hit in contacts)
 
             {
 
                 hitPosition.x = hit.point.x - 0.01f;
                 hitPosition.y = hit.point.y - 0.01f;
                 tilemap.SetTile(tilemap.WorldToCell(hitPosition), null);
             
             }
         
         }
     
     }
 
 }
Some additional information:
The way I am using this is by instantiating a game object with a circle collider 2D on it, which would then destroy the tiles it comes in contact with. (Is trigger is not enabled on the circle collider 2D since I understand that collision.GetContacts does not get any collision points if is trigger is enabled.) Other answers I have found on this subject use .contacts, but as that does not work, I believe that it has been deprecated, though I am not sure, and I know it causes a lot of memory garbage too. The documentation on .contacts says to use .GetContacts to reduce this. I replaced .contacts with setting an array to collision.GetContacts then using a foreach loop to iterate through that and disable the tiles.
Why is this not working? Any help is appreciated. Thank you!
have you tried without the world to cell ? I achieved it without it a while ago by flooring the positions according to the tilemap position.
@Tehemus Thank you, but I cannot remove the world to cell without getting an error.
 tilemap.SetTile(tilemap(hitPosition), null);
Gives me this error: Non-invokable member 'DestroyTilesOnCollision.collision' cannot be used like a method.
 tilemap.SetTile(hitPosition, null);
Gives me this error: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Vector3Int'.
Answer by Epicnez · Jul 06, 2020 at 04:34 AM
@Tehemus Got tile destruction working with this script:
 using System.Collections;
 using System.Collections.Generic;
 using System.Numerics;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class DestroyTilesOnCollision : MonoBehaviour
 {
 
     //public Tilemap tilemap;
     public ContactPoint2D[] contacts = new ContactPoint2D[10];
 
     public GameObject particles;
 
     public AudioSource explodeAndHitNoise;
 
     void OnCollisionStay2D(Collision2D collision)
 
     {
 
         Debug.Log("Hit!");
 
         if (collision.gameObject.name == "Tilemap")
 
         {
 
             Debug.Log("Hit tilemap!");
 
             int contactCount = collision.contactCount;
             if (contactCount > contacts.Length)
                 contacts = new ContactPoint2D[contactCount];
             collision.GetContacts(contacts);
 
 
             UnityEngine.Vector3 hitPosition = UnityEngine.Vector3.zero;
             for (int i = 0; i != contactCount; ++i)
 
             {
 
                 hitPosition.x = contacts[i].point.x;
                 hitPosition.y = contacts[i].point.y;
                 collision.gameObject.GetComponent<Tilemap>().SetTile(collision.gameObject.GetComponent<Tilemap>().WorldToCell(hitPosition), null);
                 var newParticles = Instantiate(particles, hitPosition, UnityEngine.Quaternion.identity);
                 var newSoundEffect = Instantiate(explodeAndHitNoise, this.transform.position, this.transform.rotation);
                 StartCoroutine(DestroyParticles(newParticles));
             
             }
         
         }
     
     }
 
     public IEnumerator DestroyParticles(GameObject particles)
 
     {
 
         yield return new WaitForSeconds(3f);
         Destroy(particles);
 
 
     }
 
 }
A redditor helped me get this working from the script in the initial question. Here's the reddit post: https://www.reddit.com/r/Unity2D/comments/gtqkuz/help_with_destroying_tiles_on_collision/ Thanks!
Your answer
 
 
             Follow this Question
Related Questions
How to the detect what side of the player is colliding with a object in a tilemap 2 Answers
Stuck Between tilemap colliders 0 Answers
Tilemap Collider 2D not being created at runtime 0 Answers
Composite Collider on Tilemap not really stopping player 0 Answers
How to detect on which exact tile a collision happened? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                