# 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?

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");
}
}
``````