CollisionIgnoreManager

From Unify Community Wiki
Revision as of 09:28, 2 July 2009 by Ryan scott (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

If no masks they default 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

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

}

</csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox