- Home /
OnTriggerEnter2D and ladder won't be compatible
I have set up my ladder climbing script to work by disabling colliders when the player is climbing. This works great and I've been happy with it.
But now I'm realizing it won't work with the "zones" I've set up in my game based on large trigger colliders which use OnTriggerEnter2D to change the ambient audio (think: going from above ground into a cave). Because the colliders are removed from the player the second he starts climbing a ladder, this is re-triggering the OnTriggerEnter2D once he's no longer climbing.
Thoughts on how to better handle a way to detect when a player enters or leaves a "zone" that doesn't rely on colliders? Or some other solution I might not have thought of?
Answer by RodrigoAbreu · Feb 09, 2021 at 11:02 PM
What about layer-based collision? https://docs.unity3d.com/Manual/Layer-based collision.html
I haven't really delved too deep into layer collision... How would that look different from the OnTriggerEnter2D function:
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Audio$$anonymous$$anager.LevelAmbienceAudio("forest");
}
}
Answer by HighCode · Feb 12, 2021 at 01:21 AM
Solved (after taking a small break)! Thanks to @RodrigoAbreu for pointing me in the right direction. My player controller script was disabling all colliders when climbing which is why when I stopped climbing, the OnTriggerEnter fired again.
Instead I switched to use Physics2D.IgnoreCollision between my player layer and the ground while climbing which works great and doesn't cause unwanted collision events to trigger!