- Home /
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: Sample Gameplay
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!
As a moderator, i can say you that this is the perfect post :D Good job! and Good luck with your problem!
Got it, thanks! Sorry for taking so long to answer, school got in the way. I used the #3 solution :)
Answer by tormentoarmagedoom · Jan 27, 2019 at 10:37 PM
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 whenr eally outside the ladder, so you make te 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!
Answer by WagnerGFX · Feb 03 at 04:24 PM
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.
Your answer
