Reputation: 61
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.
Upvotes: 6
Views: 10096
Reputation: 12258
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!
}
Upvotes: 10