How to ignore collisions between two objects but still get the notification?

Hi, I’m working in a 2D environment and I’ve got items that my character can drop.
The character drops the items inside himself, which is why I want to ignore that collision, but I want to be notified when their colliders stop intersecting each other to then turn the collision back on. As of now the collision is ignored for a specified time, which is what most people suggest on the web, but that’s not good enough. Anyone got any suggestions? Thanks in advance.

@metalted Hit the nail on the head with his collider distance solution, however it would not always be 100% accurate for complex colliders and calculating the distance when taking into consideration each collider’s rotation would add allot of complexity, However when I was further investigating collider distances I found that unity already has a function for that Physics2D.Distance(col1, col2) and that returns a handy ColliderDistance2D struct that holds this also very handy bool: isOverlapped!


So because Physics2D.IsTouching(col1, col2) doesn’t detect ignored collisions you’ll have to use Physics2D.Distance(col1, col2).isOverlapped instead.

So to ignore collisions between two objects while they’re overlapping just use this simple coroutine:

IEnumerator IgnoreCollision(Collider2D col1, Collider2D col2)
{
        Physics2D.IgnoreCollision(col1, col2, true);
        while (Physics2D.Distance(col1, col2).isOverlapped) yield return null;
        Physics2D.IgnoreCollision(col1, col2, false);
}

Hope this helps future browsers.

As you know how to toggle the collision over time, maybe you could toggle it over distance.


By saving the transform of the instantiated drop, you can later use Vector3.distance to get the distance between the player and the drop. The distance needed to clear the colliders would be : collider1.bounds.size.x / 2 + collider2.bounds.size.x / 2. This only works for spheres, so unless you are making pac-man, you might want to play around with these values. There is a lot of info to be found about collider bounds and collider extents. Eventually, when the distance threshold is reached, toggle the collision.


If you want to be sure that the collider is cleared, you could calculate the diagonal between the x-axis extents and the z-axis extents. This will give you a distance that, when drawing a line from the center of the object, will always be outside or on the collider in the x-z plane. You add the diagonal of collider 1 and 2 together to get the minimum clearing distance in the x-z plane. Then when measuring this distance, you should ignore the y-position of the colliders, to not add extra distance because of the height difference.

Im not sure if I totally understand, but if I do then you could turn it into a trigger instead, and then turn this off whenever you need for it to become a collider again?