Stop object after collision

I’m trying to stop an object after a collision.

The object is thrown by AddForce.

This is the code that should stop the object:

void OnCollisionEnter(Collision collision) {
	ContactPoint contact = collision.contacts[0];
	if (contact.normal.y <= -0.9f) {
		gameObject.rigidbody.isKinematic = true;
	}
}

It stops, but I can see that it bounce back a little before to stop. Seems to be a problem with OnCollisionEnter that is executed one frame too late.

If there is a workaround or a different solution, please let me know.

I don’t know if it would help, but you could try OnCollisionExit, as it may be a frame later like you want.

The workaround that worked for me is to store the last velocity on FixedUpdate and use it to find the new position of the object after collision, so to override the position already updated by PhysX engine.

void FixedUpdate() {
	_lastVelocity = gameObject.rigidbody.velocity;
}

void OnCollisionEnter(Collision collision) {
	ContactPoint contact = collision.contacts[0];
	if (contact.normal.y <= -0.6f) {
		gameObject.rigidbody.isKinematic = true;
		transform.position = contact.point - _lastVelocity.normalized * 30; // approximation of the radius of the object
	}
}