- Home /
Speed of objects going into collision
void OnCollisionEnter (Collision other)
{
// get the direction from player to collider
Vector3 collisionNormal = (other.transform.position - transform.position).normalized;
// get player speed towards the collision
float playerCollisionSpeed = Vector3.Dot (collisionNormal, m_Rigidbody.velocity);
if (playerCollisionSpeed < 0) { playerCollisionSpeed = 0; }
// get the other object's speed towards the collision
float otherCollisionSpeed = Vector3.Dot (-collisionNormal, other.rigidbody.velocity);
if (otherCollisionSpeed < 0) { otherCollisionSpeed = 0; }
// if player gets hit by object going faster than they hit the other object . . .
// (may change to 'if hit with more force')
if (otherCollisionSpeed > playerCollisionSpeed)
{
Debug.Log ("player got hit by faster object");
}
}
This code snippet seems to be returning the speed of the two colliding objects AFTER the collision. When hitting an un-moving object, it tells me that the un-moving object has a higher speed than did the object that caused the collision.
If I can only learn the resulting speeds, how can I figure out which of the two objects went towards the collision with more speed?
This will tell me the velocity of the two objects relative to one another. However, I am looking to see which of the two objects contributed to the collision more than the other.
Answer by erukeru · Apr 28, 2019 at 04:55 PM
https://answers.unity.com/questions/163337/velocity-before-collision.html
Getting the player's velocity before the collision, and then using that and the relative velocity lets me calculate both contributing speed to the collision.
void OnCollisionEnter (Collision other)
{
// get the direction from player to collider
Vector3 collisionNormal = (other.transform.position - transform.position).normalized;
// get player speed towards the collision
float playerCollisionSpeed = Vector3.Dot (collisionNormal, velocity);
if (playerCollisionSpeed < 0) { playerCollisionSpeed = 0; }
float otherCollisionSpeed = 0f;
// if other object had velocity towards the collision . . .
if (other.relativeVelocity.magnitude > playerCollisionSpeed)
{
// . . . get the other object's speed towards the collision
otherCollisionSpeed = Vector3.Dot (-collisionNormal, other.relativeVelocity + velocity);
}
// if player gets hit by object going faster than they hit the other object . . .
// (may change to 'if hit with more force')
if (otherCollisionSpeed > playerCollisionSpeed)
{
Debug.Log ("player got hit by faster object");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612200349im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get collision force, not relative velocity? 1 Answer
Velocity remains constant even when colliding 0 Answers
Speed after Vector3.Reflect Decreases,Speed of ball after Vector3.Reflect decreases 0 Answers
Can an object to transfer to another as it increases your speed? 1 Answer
how to reduce the speed of bike when collide with side walls. 1 Answer