- Home /
Ignore a collider when there are multiple ones?
I'm using the RobotBoy 2d standard asset for the basis of my 2d character. In another script if I execute something based on if the player collided with it, it will happen 2x because the RobotBoy by default has a Circle2d Collider and BoxCollider2d on it. Is there any way to ignore one of those specifically from executing when I do OnTriggerEnter in my other script?
Answer by Bluk · Jul 04, 2015 at 09:37 AM
You might want to look into Layer Collision matrix: http://docs.unity3d.com/Manual/LayerBasedCollision.html
Don't know how that will help because the gameobject that has the 2 colliders would only use one layer. Could you give me a example please?
You can use a game object hierarchy. In the robotboy example, you could use the groundcheck child object to add a circle collider.
Yeah i already did that for my solution. I made a Child object with a box collider2d that works with my scripts. But I want to know how to specifically choose one collider on a gameobject that has multiple ones to do something and ignore the other colliders on the same gameobject.
It is kind of strange what you are trying to do, but if i understand well what you are trying to do, you could do this:
-If the colliders are of different types you can check if it is the right collider with something like:
if(collider.GetType() == typeof($$anonymous$$eshCollider))
-If the colliders are of the same type, for example two BoxColliders, you could do something like:
collider1 = GetComponents<BoxCollider>()[0];
collider2 = GetComponents<BoxCollider>()[1];
And check if the collider is the one you want. Note that with this method, the order is deter$$anonymous$$ed by the inspector.