Ignore collisions between a certain collider and a layer

Is there a way to ignore collisions between a single collider and a layer?

I’ve found Physics.IgnoreCollision() and Physics.IgnoreLayerCollision() methods, however the first one only allows to ignore collision between individual objects, while the latter only works for entire layers.

Thanks.

There’s an API that allow you to dynamically turn off the collision between colliders from 2 different layers :

Physics.IgnoreLayerCollision

That is how you change the collisions in the Physic menu (inspector) through scripts.

EDIT - Sorry didn’t seen you have seen it already. But there’s a possible fix.

You could do a small work-around. You duplicate the single’s collider’s GameObject and place it as the child of the original GameObject. On the parent, you keep the renderer component (to show what it looks like) and on the child (the copy), you remove the renderer and only apply a Mesh Collider (or whatever collider you wish to uses). As you might have already guessed, you could keep the child (collider) in a different layer than its parent. Then you can uses the IgnoreLayerCollision to only affect the collider (child) while keeping the original (renderer + mesh) in its original layer.

Old question, but sharing my solution just in case.

For detecting and comparing two layers at runtime in a collision detector, I wrote the following extension:

public static class UnityExtensions {   
    public static bool ContainsLayer(this LayerMask mask, int layer) {
        return ((1 << (int)layer) & (int)mask) > 0;
    }
}

You can then use that in a script with a collision handler like so:

using UnityEngine;
public class MyCollider : MonoBehaviour {
    [SerializeField] private LayerMask m_Layers;

    // should work for ANY trigger or collision detector.
    private void OnTriggerEnter2D(Collider2D other) {
        if (m_Layers.ContainsLayer(other.gameObject.layer)) {
            // ignore collision or whatever you want.
        }
    }
}

From there, attach the script to your collider and then, in the editor, populate the layers field through the layers dropdown and you’re good to go!