I’m making a 2D platformer and I have platforms that disappear when the player stands on it. To detect wether the character is grounded I use OnCollisionStay2D and OnCollisionExit2D. The problem is that OnCollisionExit2D doesn’t fire when the platform disappears. I’m trying to find a way to always detect the player’s collision, even if it isn’t colliding with anything. That way if it isn’t colliding with anything I could detect that. Is there anyway to do that? Or to make OnTriggerExit2D fire when the object is disabled?
This is the relevant code:
//The solution I’m trying to implement
void OnUpdate()
{
foreach(ContactPoint2D contact in rb.GetComponent().contacts)
{
if(contact.normal == new Vector2(0, 1))
{
print(“grounded”);
}
}
}
//The Ground detection. This works fine except for the case mentioned above
private void OnCollisionStay2D(Collision2D collision)
{
foreach (ContactPoint2D contact in collision.contacts)
{
if (contact.normal == new Vector2(0, 1))
{
isGrounded = true;
break;
}
else
{
isGrounded = false;
}
}
}
private void OnCollisionExit2D(Collision2D collision)
{
isGrounded = false;
}