Help with Collision2D.relativeVelocity?

Hi -

I’m working on a 2D game and just added some moving obstacles (i.e. crushers), which is forcing a revamp of my collision logic to factor in relativevelocity. Can you help me understand the expected behavior?

Additional context:

  • Until now, the game featured a moving player navigating a maze of stationary obstacles, so I was able to use Rigidbody2D.velocity (and specifically its magnitude) to calculate collision damage.
  • But now, since obstacles can move and hit the player, I need to factor in their movement, which leads me to want to use Collision2D.relativeVelocity as my main factor.
  • (I’ve also added separate factors in related to mass, and to the dot-product of the collision relativevelocity vector vs. player velocity vector, but those factors seem to work as intended in isolation and therefore aren’t part of my question.)

So here’s my question: When the player hits a stationary obstacle, am I right that the player’s Rigidbody2D.velocity.magnitude should be the same as the collision’s Collision2D.relativeVelocity.magnitude?

That’s what I’d expect, but the two aren’t identical, and in a way that seems inconsistent and unclear. What factors would cause them to be different?

(FWIW, I’ve updated to 5.3.4p2, as I heard that was supposed to include fixes to Collision2D.relativeVelocity calculations.)

Thanks for whatever help you can provide!

Slapworth

Inside the collisionEnter2d method:

rb2d.velocity is the velocity that the object has AFTER colliding.
relativeVelocity is the difference between the two objects velocities BEFORE colliding.