CollisionIgnoreManager

From Unify Community Wiki
Jump to: navigation, search

There is currently no way to ignore collisions between groups of objects. When objects add themselves to this manager they provide two bitmasks, one specifying what groups they belong to and another specifying which other objects they would like to ignore collisions with.

The default behavior is to ignore all other objects.

A simple usage case would be putting various objects onto different layers and using the layer masks for the bitmasks.

C# - CollisionIgnoreManager.cs

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 );
    }
}

Feel free to ask me any questions regarding this script

 -Ryan Scott-
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox