- Home /
Question by
TruffelsAndOranges · Sep 15, 2015 at 01:25 PM ·
layermask
Get LayerMask that current GameObject collides with?
Basically I have a MonoBehaviour script which I make a sphere cast with. Currently I've set it up so that I have a public LayerMask that I set manually. But I just manually set it to the layers that the game object currently collides with! It'd be so much nicer if there was an easy way to get the current collision mask for the current game object through code. How can I do this?
Comment
Can you describe your intention a little more? What do you want to do with it?
I want to sphere cast using the collision mask of a arbitary game object.
Best Answer
Answer by TruffelsAndOranges · Sep 15, 2015 at 03:59 PM
This code works well:
public static LayerMask GetCollisionMaskOf(GameObject go)
{
int myLayer = go.layer;
int layerMask = 0;
for (int i = 0; i < 32; i++)
{
if (!Physics.GetIgnoreLayerCollision(myLayer, i))
{
layerMask = layerMask | 1 << i;
}
}
return layerMask;
}