- Home /
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.
$$anonymous$$ake two layers , one layer for one collider and other layer other object
For example "Player" layer for player , "IgnoreCollisionLayer" for which layer want to ignore
Set layer for player and other collider
Go to Edit > Project Settings > Physics
In Physics manager you can see Layer Collision $$anonymous$$atrix ( Triangle shape checkboxs)
Disable collision between "Player" and "IgnoreCollisionLayer"
Thanks for your answer. Yes, I thought about that too. However the problem with this approach is that it does not allow to dynamically (i.e. at runtime) designate certain single collider to ignore collisions against a layer. I guess one could programatically assign an object to a layer, but this solution is not always applicable (what if there are several collider-layer pairs that need to be excluded from collision detection?).
Im new to all this unity develop thing and this was a really big help, so thanks :)
Answer by Max_Bol · Jun 24, 2015 at 01:18 AM
There's an API that allow you to dynamically turn off the collision between colliders from 2 different layers :
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.
Answer by hjc_ · May 20, 2020 at 08:28 PM
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!