Physics.IgnoreLayerCollision question

Hi,

I need an object to collide ONLY with objects in a specific layer.

looking the Physics.IgnoreCollision I should be able to do it using

Physics.IgnoreLayerCollision(1<<gameObject.layer, (0xFFFFFFFF  (~(1<<target_game_object.layer))), true);

but this gives me an error in the console saying that it cannot convert a long into an int on the second parameter O_o
i’m quite sure “8” F means 32 bits, anyway… it could be because the first 8 bits are reserved by unity so i thought using 0xFFFFFF instead (removing “two F”) should do the trick and in that case it compiles but when it try to execute it gives me another error

“layer numbers must be between 0 and 32”

That basically means that in this case layer do not work as a bit mask but the function wants the actual layer number… and anyway shouldn’t the error say “between 0 and 31”?

An easy way is doing what i need would be something like

int my_layer = gameObject.layer;
for (int layer=31; layer>=0; layer--)
{
  Physics.IgnoreLayerCollision(my_layer, layer, true);
}
Physics.IgnoreLayerCollision(my_layer, target_game_object.layer, false);

but i really hope there’s a cleaner way to achieve it

Any thoughts/helps will be appreciated.

Thanks,
A

I think whats happening is that your int is overflowing after the “<<”-operator and C#/Unity turns it automaticly into a long.
A combination of unchecked() and Typecasting should make it work.

Nope no better way to do it. The only think i can think of is since it says a number between 0 and 32, maybe 32 is like all layers. But either way you shouldnt use it since its not documented.

heres shorthand
for(int i=0; i < 32; i ++) Physics.IgnoreLayerCollision(my_layer, i, i != target_layer );

You do use the layer bit number and not the mask.

A cleaner way to do this without dealing with layers would be to configure things in a way you wouldnt need to mess with this in teh physics settings.

it’s a shame that we have a bitwise layer mask and we have to use it like a normal int with functions like this.

what’s the point of having a bit mask if we can’t use it in the proper way?

thanks anyway

The only time you use bitwise with layers is for collisionMasks on the overlap or cast functions. I think its to be more simple, but yea i agree its kind of over simplification which ultimately results in complication in some areas more than others.