Ignore collisions with objects of same type

Hi,
I’m trying to get two or more instantiated objects of the same type to ignore collisions with each other, but not with other objects. I’ve tried Physics.IgnoreCollision, but I get the message “The two shape references must not reference the same shape.”

Is there another way to do this?

So, don’t make objects use Physics.IgnoreCollision try to ignore collisions with themselves, only other objects.

–Eric

I used Physics.IgnoreCollision and it worked just fine. Get all the objects you want to test, loop through them and if object != the object that’s ignoring the collision AND it’s tag/label == “something”, let them ignore the collision.

Place all the objects into a layer, and then when casting the ray do something like

//ignore layers 8 and 5
var layersToHit : int = ~((1<< 8) + (1 << 5))

if (Physics.Raycast(source,dest,outInfo,layersToHit))
{
//do stuff
}

Hmmm… Maybe I didn’t explain well enough. So what I have are basically a lot of balls flying through the air. I don’t want the balls to bounce off each other, but I do want them to be able to hit other things. I tried:

Physics.IgnoreCollision(Ball.collision, collision);

Ball is the GameObject that I assigned the prefab earlier. That didn’t work. Is there a way I can make it so that these balls, which are just instantiated versions of the same prefab, pass through each other, but not through other objects.

Easy fix:

if(Ball.collision != collision)
{
Physics.IgnoreCollision(Ball.collision, collision);
}

Now you won’t ever set an ignore collision to itself.

Although, you will likely need to store a List of these balls to iterate through to disable collisions;

i.e.

balls is a List of all your Ball GameObjects.

for(int i = 0; i < balls.Count; i++)
{
for(int j = 0; j < i; j++)
{
Physics.IgnoreCollision(balls*, balls[j]);*
}
}
This is setup to only be called once, if you want to add balls at random times, you will just want to call ignore collision between the new one and all existing ones each time.