I am new to unity and am making this little platformer where I just started trying to add coins as a tilemap, I found a helpful video on destroying tiles but it only works with OnCollisionEnter, and since I am making coins I figured it has to be a trigger.
It stops working right at collision.contacts and says ‘collider2D’ does not contain a definition for ‘contacts’
I’m looking for an alternative to collision.contacts but for OnTriggerEnter
here’s the code:
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Vector3 HitPosition = Vector3.zero;
foreach(ContactPoint2D hit in collision.contacts) // <-- doesn't work with OnTriggerEnter...
{
HitPosition.x = hit.point.x - 0.001f * hit.normal.x;
HitPosition.y = hit.point.y - 0.001f * hit.normal.y;
destructableTilemap.SetTile(destructableTilemap.WorldToCell(HitPosition), null);
}
}
}
when you do ctrl + . it just says to change collision.contacts to collision.GetContacts… which also doesn’t work
UPDATE:
Thank you for the reply, and it works(!), But it only will delete a coin upon touching it from the left, right or up, but if you are going down onto the coin it wont delete it,
here’s the changed code:
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(other.transform.position);
if (other.gameObject.CompareTag("Player"))
{
Vector2 HitPosition = Vector2.zero;
HitPosition = other.transform.position;
{
HitPosition.x = (HitPosition.x) - (0.01f * HitPosition.x);
HitPosition.y = (HitPosition.y) - (0.01f * HitPosition.y);
destructableTilemap.SetTile(destructableTilemap.WorldToCell(HitPosition), null);
Debug.Log(HitPosition);
}
}
}
I’m pretty sure it only isn’t working because of the - (0.001f * HitPosition) because it seems to me that it will move the point where it deletes a tile a little up and to the left which is supposed to make sure it gets the right tile but from the top it doesn’t work.
Again thank you for the reply