Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting collision contact force

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.

like image 658
Gökhan Demir Avatar asked Jan 07 '23 08:01

Gökhan Demir


1 Answers

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!
}
like image 88
Serlite Avatar answered Jan 08 '23 21:01

Serlite