Grouping objects for collision ignoring.

Hi! I know that i can have two object not to collide by using physics.ignorecollision.

But what i would like to do is have many objects of a same class not to collide between each other... isn't there a way to make a collision group so this objects don't collide?

If it is not posible how can i call physics.ignorecollision for all of them in an efficient fashion?

Thanks!

This is kind of the same question as this one, which I think I've answered, although it has actually been asked later than this one ^^.

There's currently no built-in way.

There's a feature request for this on the Unity Feedback Forum which you could add your vote to: http://feedback.unity3d.com/pages/15792-unity/suggestions/163341-physics-layer-based-ignore-collision

In the meantime, you could use this neat collision manager script on the wiki, written by Ryan Scott.

Right now i am doing this:

function OnCollisionEnter(collision : Collision) {

if (collision.collider) {
	if(collision.gameObject.tag == "Bullet")
	{
		Physics.IgnoreCollision(rigidBody.collider,collision.collider);

	}
	if(collision.gameObject.tag == "Player")
	{
	}
}

}

Don't know if that is "efficient" but works...

I used this with Kinematic Rigidbodies (that weren't controlled by physics) and used OnTriggerEnter instead of OnCollisionEnter, but it may work in this case also.

If you're looking to make all objects of the same class not collide you could create a new "myClass" tag for them and in the OnCollisionEnter function add:

function OnCollisionEnter (col : Collider) {
	if (col.gameObject.tag == "myClass") {
	} else {
		Destroy(gameObject);
	}
}


If you want only some instances of this class ignore and not others, you can change that to:

function OnCollisionEnter (col : Collider) {
	if (col.gameObject.tag == "myClass") {
		if (!ignoreCollision) {
			Destroy(gameObject);
		}
	} else {
		Destroy(gameObject);
	}
}

then add the ignoreCollision variable to the class' script:

private var ignoreCollision : boolean = false;

To set an instance of the class to be ignored, you can create a function in the class' script:

function SetIgnoreCollision() {
ignoreCollision = false;
}

and then call it upon instantiation in whatever script handles that:

newMyClass = Instantiate(myClass, Vector3(0,0,0), Quaternion.identity);
newMyClass.SendMessage("SetIgnoreCollision");

For those who stumble upon this question since the more recent versions of Unity…

You can achieve group collision ignoring by going to Project Settings, and then either Physics or Physics 2D. In the Inspector window, you’ll see a Layer Collision Matrix, where you can check or uncheck which layers you want to collide with each other.

Then, of course, just make sure your game objects are assigned to the correct layer!

I use this

using UnityEngine;

public static partial class ExtPhysics
{
	#region IgnoreCollision
	public static void IgnoreCollision(params Collider[] colliders)
	{
		IgnoreCollision(true, colliders);
	}

	public static void AllowCollision(params Collider[] colliders)
	{
		IgnoreCollision(false, colliders);
	}

	public static void IgnoreCollision(bool ignore, params Collider[] colliders)
	{
		for(int i = 0; i < colliders.Length - 1; i++)
		{
			for(int j = i + 1; j < colliders.Length; j++)
			{
				Physics.IgnoreCollision(colliders*, colliders[j], ignore);*
  •  	}*
    
  •  }*
    
  • }*
  • #endregion*
    }
    Example of its usage
    using UnityEngine;

public class TestIgnoreCollisions : MonoBehaviour
{

  • public Collider colliders;*

  • public bool ignore;*

  • void Update()*

  • {*

  •  if(ignore) ExtPhysics.IgnoreCollision(colliders);*
    
  •  if(!ignore) ExtPhysics.AllowCollision(colliders);*
    
  • }*
    }
    Note that although I am calling this every Update, its only for demonstration purposes.
    You should only call it when needed.