How to set a layermask with two layers?

Basically I want to use an OverlapSphere that can only collide with two layers named “GroundEnemy” and “FlyingEnemy”. I know that if I only want one layer, I could use…

1 << LayerMask.NameToLayer(“GroundEnemy”)

And it would only hit GroundEnemy. But I want it to only be able to hit FlyingEnemy as well as GroundEnemy.

Thanks in advanced

int grnd = 1 << LayerMask.NameToLayer("GroundEnemy");
int fly = 1 << LayerMask.NameToLayer("FlyingEnemy");
int mask = grnd | fly;

plus would “technically” work, but if you have 2 masks that you want to stick together… and both share the same layer already… + will double them up and make the next layer up from it. Boolean OR doesn’t have this issue.

3 Likes

Thanks!

Make a public LayerMask variable, then you can just set it in the inspector and don’t need to use strings.

var myMask : LayerMask;
public LayerMask myMask;

–Eric

2 Likes

LayerMask.GetMask(“GroundEnemy”, “FlyingEnemy”)

6 Likes