Getting collision contact force
Asked Answered
B

1

6

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.

Breger answered 3/4, 2016 at 16:6 Comment(1)
Hi, did you ever get your problem resolved? If not, please update your question to indicate how existing answers have not successfully answered your question. Thanks!Leucas
L
10

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!
}
Leucas answered 4/4, 2016 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.