OnTriggerExit2D calling when object is inside collider

Hi,

Short version:

How can detect if one collider is inside another and not touching any of the collider borders?
Both colliders are triggers have rigid bodies. Read long version if you need more info.

Long version:

I have a 2D platformer game where my character is supposed to be able to climb ladders. I am doing this by checking when the player collider enters the ladder collider. When the player exits the ladder collider I am running OnTtriggerExit2D. My problem is that if my player is perfectly centered on the ladder and he starts climbing, his entire collider is inside the ladder collider and since the colliders aren’t overlapping the OnTriggerExit2D is fired.


Se the video for the problem in action:

Both the colliders are triggers and have rigid bodies. One capsule collider and one composite collider.
I know I can make the ladder collider thinner so the player always hits one of the sides, but I can’t figure out how to do that with a composite collider and it wouldn’t be a proper solution anyways.

Here is the trigger code:

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Climbable"))
    {
        playerController.climbing = true;
    }
}

private void OnTriggerExit2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Climbable"))
    {
        playerController.climbing = false;
    }
}

Is there a simple fix for this?

Thanks in advance!

I had the exact same issue and stumbled on this question, but ended up with a different solution. Since the question is from 2019, this might no have been available at the time.

My Solution

If you are using a Tilemap with the TilemapCollider2D and the CompositeCollider2D set as a Trigger then you should change Geometry Type to Polygon, otherwise the detection will only occur when overlapping with the Outline.

Good day.

Question #1

Did you tried OnTriggerStay?

Not the best solution #1

When you detect OnTriggerEnter, make the player trigger a little bigger to prevent that. So OnTriggerExit will only be executed when really outside the ladder, so you make the player trigger smaller again.

Not the best solution #2

Calculate the distance from the ladder to the player before execute whats inside the OnTriggerExit.

Possible solution

Create a new collider in the center of the ladder (by instantiating a prefab empty gameobject with only that collider. This methods also work with colliders of child objects)

Bye!

I believe that Unity’s 2D trigger stack may be bugged. I submitted a bug report related to this.

If this is somehow “as designed”, I think it’s poorly documented and/or designed, at least from a DevX standpoint.

I have a situation where OnTriggerExit2D and OnTriggerEnter2D are just getting called in a circle. This makes no sense.

In the meantime, I am going to check for overlapping.