CollisionIgnoreManager

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Ryan scott)
Line 1: Line 1:
 +
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==
 +
<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>

Revision as of 19:21, 19 October 2009

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

<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