- Home /
How to make colliders to react only with one layer?
Hi, Is there collision layers in unity or any other way to separate different groups of colliders?
Answer by jonas-echterhoff · Feb 16, 2010 at 07:48 AM
Currently there is only Physics.IgnoreCollision(), which acts per collider, not per layer, so, likely you have to write some more complex setup code for what you want. We are working on improving this in the future.
@test84: Physics layers have been available for a long time (3.4 or possibly earlier).
Answer by JDonavan 1 · Feb 19, 2010 at 12:26 AM
Ryan Scott posted a script to the UnifyWiki site that might help...
From http://www.unifycommunity.com/wiki/index.php?title=CollisionIgnoreManager
using UnityEngine; using System.Collections;
// Anything provided to this manager will have its collisions with anything else registered
public class CollisionIgnoreManager : MonoBehaviour {
public static CollisionIgnoreManager collisionIgnoreManager = null;
ArrayList ignoreObjects = new ArrayList();
ArrayList ignoreMasks = new ArrayList();
public static CollisionIgnoreManager getSingleton() {
return collisionIgnoreManager;
}
// Use this for initialization
void Start () {
if( collisionIgnoreManager == null )
collisionIgnoreManager = this;
}
// Update is called once per frame
void Update () {
// clean up any dead objects
for( int i = ignoreObjects.Count - 1; i >= 0; i-- ) {
if( ignoreObjects[ i ] == null ) {
ignoreObjects.RemoveAt( i );
ignoreMasks.RemoveAt( i );
}
}
}
public void addIgnore( Collider newCollider ) {
addIgnore( newCollider, 0xffff, 0xffff );
}
public void addIgnore( Collider newCollider, int thisMask, int mask ) {
for( int i = 0; i < ignoreObjects.Count; i++ ) {
Collider collider = ignoreObjects[ i ] as Collider;
if( collider != null && ( mask & ( (int) ignoreMasks[ i ]) ) == mask )
Physics.IgnoreCollision( newCollider, collider, true );
}
ignoreObjects.Add( newCollider );
ignoreMasks.Add( thisMask );
}
}
Ins$$anonymous$$d of a for
loop, why not use a foreach(Collider collider in ignoreObjects)
?
Answer by stijn · Sep 28, 2011 at 03:13 PM
I realize this was written a while back but somehow this post still comes up when searching.
Unity now has implemented the following method:
For example:
// ignore collisions between first 2 custom layers
Physics.IgnoreLayerCollision( 8, 9 );
I realize this was written a while back but somehow this post still comes up when searching.
+1 (in spirit, I don't have the karma!) I found this question in search, so thanks for that!
Your answer
Follow this Question
Related Questions
Delayed Collisions Bug continued... 0 Answers
Collisions between two rigidbody's and scripting collisions problem 2 Answers
How can I change this script? 0 Answers
Handling collision in child object 1 Answer