# Ignoring collisions without using physics layers [duplicate]

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?

• That's not the same... I want both items to simply go through one another, ignoring each other. no need to initiate a collision, unlike his question which he wanted a collision event. – tbkn23 May 15 '13 at 16:56
• Ignoring collisions can easily be done by responding to a collision by doing nothing. Collisions without a collision response is the same as ignoring the collision. If you're able to decide what to do when a collision is detected, that's all you need. Just decide to do nothing. – MichaelHouse May 15 '13 at 17:20
• @Byte56: But the physics engine does some things regardless of what I do in my code. The collision resolution system alters the velocity of objects that collide. I was trying in my code to tell the collision system to ignore this collision, but apparently it's too late to do it in the OnCollisionEnter. – tbkn23 May 15 '13 at 17:32
• @tbkn23: you seem to be saying that you want a different answer than what the answer is. The answer is to use layers. If you don't want to use layers for this, you're out of luck. That's how Unity solves this particular problem. – Sean Middleditch May 29 '13 at 23:54

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

When you have specific objects pair that you don't want to collide, using layers is definitively the best way to go.

creating a layer for each prefab would complicate the layer matrix more than I want to.

"Complicate" the layer matrix is not a problem. The matrix is limited to 32 layer, if you haven't free layers to use than you might consider other solution. Until I have a free layer I'd always go for using it.

Physics.IgnoreCollision works just fine. There are some limitations documented indeed.

Btw, the problem is that you are calling it when a collision as already occurred. You have to use it on an object pair you want ignore collision for, before the collision occurs(for example set up it inside Start method instead).

• The problem is that calling it before the collision occurs requires instantiated objects, so if I have 100 bullets in the air I'm going to have to call every pair of them. I imagine that could have a bad impact on performance. Layers is OK but the 32 limit for the entire project is a bit limiting... if it was 32 per scene it would have been better, that's why I'm looking for another solution. – tbkn23 May 15 '13 at 21:27
• @tbkn: yes you have to call it for every pair, but I think the bullet should not collide only with the gun. It's quite improbable that a bullet could ever collide with another bullet. Btw if you don't want to use layers this is the only way to go. – Heisenbug May 16 '13 at 8:16

Ok, so I haven't seen anyone answering with a solution for ignoring collusion between certain prefabs without assigning them their own layers (which are limited to 32).

So it's clear that in current unity versions, you must call IgnoreCollision() for each pair of instantiated objects.

So the solution I found, is an old one (before the layers were introduced), but it seems like it can work. It's called a "collision manager", and it's basically a class that stores references to all the objects that are instantiated, and calls IgnoreCollision() for every pair of objects of types that should not collide. To make it work each object has to register itself with the collision manager and it's type in the Start() method. This way the collision manager can make sure that every pair of objects of types that shouldn't collide will not collide, assuming all objects register themselves.

Here's the link to the article I found, although it's very lacking in explanation:

http://wiki.unity3d.com/index.php?title=CollisionIgnoreManager