Is there a way to get contact force of a collision? I tried to do this by getting the velocity in the OnCollisionEnter()
method. But it's giving velocity after the contact, which isn't useful to me.
Absolutely! If you check the Unity docs, there is a handy variable called Collision.impulse
. This was only introduced recently in Unity 5.2 so if you haven't updated yet, consider doing so. (Otherwise, you'll be forced to use one of the now-deprecated solutions floating on the internet instead.)
Based on the documentation, to get the force applied you would just divide this value by the last frame's Time.fixedDeltaTime
(since in physics, impulse = force * time
):
void OnCollisionEnter(Collision col) {
Vector3 collisionForce = col.impulse / Time.fixedDeltaTime;
// And now you can use it for your calculations!
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With