I have an ability in my game in which the player can dash towards the mouse by pressing a button. I wanted to make the player able to go into the tilemap of the ground during this, and just get forced out after the dash is over and they are still in the ground. The tilemap uses a composite collider in order to have all of the tiles act as one collider. I did the dig by using OnCollisionStay2D, if the object has the ground layer, then setting the composite collider’s “is trigger” to true, and then in OnTriggerExit2D, if we are inside the ground layer, set the object’s collider isTrigger to false. If the dash ends inside the ground, they are forced up out of the ground until they are fully out.
However, this doesn’t seem to work on the tilemap. Dashing into the tilemap results in both the OnCollisionStay and OnTriggerExit being called in the same frame or very soon after, I assume after the player enters a new tile within the tilemap. I confirmed with debug.log that the TriggerExit is being called well before the player exits the tilemap.
This entire system works with a box collider though, the collisionstay being called at the right time, and then the player being forced out and setting the collider’s trigger when the player leaves the box.
Does anyone know why CollisionStay and TriggerExit would be called at nearly the same time? This results in the player entering the collider, it does set trigger to on, the player enters, and then trigger exit is called, setting the collider back, and making the player stuck inside of the tilemap.
I believe the issue was the separate tiles behaving as separate colliders, but I fixed the issue by setting the composite collider’s geometry type from “outlines” to “polygons”.
FYI: A TilemapCollider2D is a single collider and behaves the same as every other collider with regards to callbacks. Using a CompositeCollider2D is the same too, it’s a single collider.
Any collider can be comprised of multiple shapes apart from the Circle, Box & Capsule colliders which simply create a single shape.
You only get an enter when you first contact a collider shape and exit when you are no longer in contact with any shapes for that collider. This is the same for all collider types; there are no special handling depending on what type it is. Callbacks are much lower-level than Unity colliders.