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!