Search Unity

OnCollisionExit2D relativeVelocity zero?

Discussion in 'Physics' started by Nicolas1212, Dec 18, 2017.

  1. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    139
    Hi,

    We were using OnCollisionExit2D to play some FX and SFX for our game and recently noticed they weren't playing any more. Looking into it, it's because the collision.relativeVelocity returned in OnCollisionExit2D is (0.0, 0.0). It's correctly (or seems to be) reported in OnCollisionEnter2D.

    Is this a bug or a recent change? We're currently on 5.6.3p4, but unfortunately I can say which release the problem appeared in, as we're only noticing it now.

    Thanks
     
  2. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    139
    To shed further light onto this issue:

    Part of our (golf) game has blocks that you can break when you hit them.

    If you place your ball beside one of these blocks so that they start off touching, then shoot the ball towards the wall:
    • OnCollisionExit2D is called *twice* - to "destroy" the block, we're just calling SetActive( false ) on the GameObject, but in both cases, activeSelf is true for the block
    • In both cases relativeVelocity is (0.0, 0.0)
    • The velocity of the ball is (0.0, 0.0) (I'm assuming as it's just started moving)
    • The contacts array is empty (because the ball never *struck* the block, but started touching it?)
    The issue with this is that it lets players break two blocks in a row if they start their ball right beside one. I'd reflect the velocity myself, except that, as the contacts array is empty, there's no way to get the normal of the wall.

    Has anyone come across this before, or has a workaround?

    Thanks,