Ignoring collisions without using physics layers

Hi,
I tried this question in Game Dev of Stack Exchange, but it might get more eyes out here…

I’m trying to ignore collisions between certain prefabs, but in this case I don’t want to use layers since these are specific object pairs that I want to not collide, and creating a layer for each prefab would complicate the layer matrix more than I want to.

What I tried so far:

public void OnCollisionEnter(Collision collision) {
    if (collision.gameObject.tag == "Bullet") {
        Physics.IgnoreCollision(collision.collider, collider);
    }
}

So I tried ignoring the collision in the OnCollisionEnter, but this only worked partially. The objects did go one through the other, but they both slowed down. My guess is that the first frame of the collision, the one that generated the call to OnCollisionEnter, was calculated regardless of the ignore command.

Any ideas how I can make all bullets not collide to one another, without putting them in a separate layer?

Depending on what should happen on collisions, you could set the projectile colliders to triggers and use the OnTriggerEnter event to process the collision yourself. This works fine when the projectile simply destroys the item hit, but probably will fail miserably if the goal is to produce an impact: a rock hitting a car, for instance, should throw the car away and modify its own trajectory as a consequence of the impact, but such effect is hard to emulate in your own code.

Turning back to the layers, you can set the collision matrix by script with Physics.IgnoreLayerCollision - this way all you would have to do is to set a different layer for each projectile type in the prefab script and disable collisions inside the this layer. A simple script like the one below could do the job:

public int myLayer = 11; // define the layer in the prefab script (8 to 31)

void Start () {
	gameObject.layer = myLayer; // move the object to its layer...
	// and ignore collisions between objects in it:
	Physics.IgnoreLayerCollision(myLayer, myLayer, true);
}

Just set the layer number in the prefab script, so that all instances of this object will be in the same layer and ignore collisions between them. You don’t need to define the layers in the Layer Manager - they already exist, but don’t have names associated and thus won’t appear in the Layer Collision Matrix.