How to detect on which exact tile a collision happened?

I have a tilemap with a TIlemapCollider2D with it’s trigger on true, and a script that detects the collision with the tilemap through OnTriggerEnter2D.
It detects collision, but I wan’t to know the exact position of the Tile that detected the collision.
Is there any way to do that?

For starters, you need the collision point. OnTriggerEnter2D is very problematic on this aspect, since it doesn’t provide collision data, only the collider. So you don’t have the contact points. You can either calculate them manually based on the position of the hitting object, or manually do a physics raycast on the collider to get the collision info. This is a bit of an oversight on Unity’s side. Another option is to use a non-trigger collider and make the rigidbody kinematic, so it will not be affected by forces. See here. If you use OnCollisionEnter2D then you can use collision.GetContacts() to find the contact points.

Now, once you have the contact points, you can use the following:

@Tomer-Barkan wouldn’t the tiles be solid if IsTrigger is off, even if it has a kinematic rigidbody? When I try it it is, and the whole point of IsTrigger being on in the first place is so that other objects can go through it.

It may be related to newer Unity versions replacing the isKinematic box with the Dynamic/Kinematic/Static options, but idk how to do it otherwise. I was using OnTriggerEnter and using the ClosestPoint function with the player’s position but it didn’t seem to be reliable.

Edit: I found that as long as the collider of the tile is not touching the edges, and the player is less than a tile big, this approach does work, but it’s certainly not perfect.