How to combine two or more layers in Physics2D.OverlapCircle

I’m using Physics2D.OverlapCircle to know if the player is grounded or not. If “isGrounded” it’s true, the animations plays, the player can jump, etc. However, I need to have more layers in the layerMask (the ground and platform layers), but I don’t know how to combine them so that the program knows that I want “isGrounded” to be true when the player it’s stepping on the ground or a platform layer. Here is my code:

[Space]
[Header("Ground check variables")]
public Transform groundCheck;
public LayerMask groundLayer;
public LayerMask platformLayer;
public bool isGrounded;
public float groundCheckRadius;

void Grounded()
    {
        var groundCollider = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer); // groundLayer is where I need to combine more layers
        isGrounded = groundCollider != null;
    }

Any hint in the right direction or documentation is really appreciated!

You can just select multiple layers in the inspector.

1 Like

Maybe use LayerMask.GetMask?

Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, LayerMask.GetMask("Ground", "Platform"));

Btw, here 's an extension for excluding layers.

        public static int GetMaskWithout(params string[] layerNames)
        {
            var excludeMask = LayerMask.GetMask(layerNames);
            return GetMaskWithout(excludeMask);
        }

        public static int GetMaskWithout(int excludeMask)
        {
            var everyLayer = ~0;
            return (everyLayer &= (~excludeMask));
        }
1 Like

As above, you only need a single LayerMask. A layer mask is a mask where each bit is a single layer so it is already set-up to select multiple layers. Presumably you selected the ground-layer in the inspector, just select the platform layer too.

1 Like

Thanks!