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.
Getting collision contact force
Asked Answered
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
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!
}
© 2022 - 2024 — McMap. All rights reserved.