Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Possible platformer effector bug

Discussion in 'Physics' started by willqp, Jan 7, 2016.

  1. willqp

    willqp

    Joined:
    Dec 3, 2010
    Posts:
    12
    I'm developing a 2D platform game that uses the platform effector. I use the function OnCollisionEnter2D to set variables like isGrounded and to change the animation state in Mechanim.Since platform effectors trigger that function even when the collision is ignored, I check the variable Collision2D.enabled to make sure that the collider is enabled and the character is actually on top of the platform. This works fine most of the time. However, I realised that sometimes the collision is resolved and the player stays on top of the platform, but OnCollisionEnter2D doesn't get called. After some debugging I think I found the problem: most times, when the player jumps it triggers OnCollisionEnter2D when it touches the platform from below and I ignore that collision because Collision2D.enabled is false. The player continues to move up until it goes over the platform and then falls on the platform and triggers OnCollisionEnter2D again and this time I set isGrounded to true because this is a valid collision. This is the most common behaviour. However, there is a special case that happens when the platform is at a height equal to the height of the jump. In this case, the collision is resolved but the player hasn't exited the collision, so the player stays on top of the platform but OnCollisionEnter2D doesn't get called because it was called when the character hit the platform from below. As a result, I had to also implement the OnCollisionStay2D function to check for Collision2D.enabled until it becomes true to set isGrounded to true. This is obviously not an ideal solution. I don't know about other games, but in my case, triggering OnCollisionEnter when the collision is ignored is clearly not the desired behaviour, or, at least, it should get called again when the collision is enabled. Or maybe I'm missing something. Any ideas?