Cancel a collision


I'm trying to have different type of "Materials" collide with each others, with different effects depending on the types of the 2 colliding objects. For example, an object with material A should get destroyed when colliding with one made from material B, but not when colliding with material C.

The problem I have is that if I do the materials check in OnCollisionEnter, the collision has already happened, and even if I Destroy one of the colliding GameObjects, the other one still reacts to the collision.

What I'm looking for is a way to cancel a collision. When I destroy one of the colliding object, the other one should stay in it's play as if nothing happened.


You could have a variable that keeps the velocity from the previous frame, and if the materials check doesn't pass, restore the velocity from the variable. Also probably another variable for angular velocity.

I would like to add to Eric5h5’s answer for those that look up this problem later on.
keeping the previous velocity and angular velocity is the only way of doing this, I’ve tried plenty of other options. However, on impact not only does the velocity get affected by the impact but the object’s position as well, immediately in the same frame. So you want to rectify that like so:

Physics.IgnoreCollision(myCollider, collision.collider);
rb.velocity = previousVel;
rb.angularVelocity = previousAngVel;
transform.position = previousPos + rb.velocity * Time.deltaTime;

Without the last line you will indeed keep your velocity but stop at the other collider for a visible frame, ruining the flow and looks clunky.