Maintain velocity after impact with kinematic rigidbody

So right now this seems to be working for me but I wanted to check to see if it’s the best way to do this. I want to hit a kinematic rigidbody that tests the collisions to see if the relativeVelocity.magnitude reaches a high enough level to break the object and spawn pieces. If the check passes I want the collider to maintain its velocity after the collision with the kinematic rigidbody…so I’m doing that by directly changing the collider.velocity to the collision.relativeVelocity. Seems to work in my limited testing but I wanted to make sure there’s not a better way to do this.

Thanks in advance :slight_smile:

var ShaperBlockPrefab : Transform;

function OnCollisionEnter(collision : Collision) {
   if (collision.relativeVelocity.magnitude > 100){
var spawnPoint : Vector3 = Vector3(transform.position.x, transform.position.y -18, transform.position.z);
Instantiate (ShaperBlockPrefab, spawnPoint, transform.rotation);
collision.collider.rigidbody.velocity = collision.relativeVelocity;
Destroy(gameObject);
}
}

Relative velocity is your speed minus their speed, so only works if the “smashees” aren’t moving (if one was moving towards you, the relative velocity is higher, so you speed up after the hit.)

My current untested solution is to save oldVelocity each frame(in fixedUpdate) and restore it after the crash, as needed. Either that or petition UnityDev to make a “OnPreCollisionEnter()” which is called just before physics is applied, with a “skipPhysics” optional flag.