How to get all collided tiles in tilemap collision?

Viewed 325

I have a Tilemap collider 2D (trigger) and capsule collider 2D (trigger and has a rigidbody2D). I succesfully get the collission in my OnTriggerEnter2D method. But have no clue in how I can retrieve all tiles that collided with the capsule collider.

In the first picture you can see the capsule collider and in the second the tilemapcollider. I want to color all tiles that collided with the capsule collider.

Capsule collider 2D (green line)

Tilemap collider

What I've already tried I tried to get all contact points using the GetContacts function. But I get a count of 0.

private void OnTriggerEnter2D(Collider2D collision) {
    GameObject go = collision.gameObject;
    if (LayerMask.LayerToName(go.layer) == "ground") // is collision with TileMap
    {
        ContactPoint2D[] contacts = new ContactPoint2D[100];
        int contactCount = collision.GetContacts(contacts);
        Debug.Log(contactCounts); // 0

        for (int i = 0; i < contactCount; i++)
        {
            Vector3Int tile = walkableTilemap.WorldToCell(contacts[i].point);
            walkableTilemap.SetColor(tile, newColor);
        }
    }
}
0 Answers
Related