How can i get a layer's collision mask?

I want to cast a ray with the same collision properties that it would have, if it were an object on a specific layer. The raycast should hit anything that object would collide with, and ignore everything that object would pass through.

I can manually copy this information and hardcode it, but that seems wrong, especially since i’d have to update that if i change the collision settings later

Is there any script command where i can specify a layer, and it’ll return a layermask for how that layer collides with others?

There’s a method named Physics.GetIgnoreLayerCollision(int layer1, int layer2). It returns true if layer1 and layer2 doesn’t collide.

So to set up the layermask for your raycast, you would get the layer of your object, and then create a layermask with a for-loop:

int myLayer = gameObject.layer;
int layerMask = 0;

for(int i = 0; i++; i < 32) {
    if(!Physics.GetIgnoreLayerCollision(myLayer, i))  {
        layerMask = layermask | 1 << i;
    }
}

That should give you the correct layermask.