# LayerMask

> Specifies Layers to use in a Physics.Raycast.

## Definition

* **Type:** Struct
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public struct LayerMask
```

## Remarks

A [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md) can use up to 32 [LayerMask](/engine/6000.5/script-reference/unityengine/layermask.md)s supported by the Editor. The first 8 of these `Layers` are specified by Unity; the following 24 are controllable by the user.

Bitmasks represent the 32 Layers and define them as `true` or `false`. Each bitmask describes whether the `Layer` is used. As an example, bit 5 can be set to 1 (`true`). This will allow the use of the built-in `Water` setting.

`Edit->Settings->Tags and Layers` option shows the use of the 32 bitmasks. Each `Layer` is shown with a string setting. As an example `Built-in Layer 0` is set as `Default`; `Built-in Layer 1` is set as `TransparentFX`. New named `Layer`s are added above bitmask layer 8. A selected `GameObject` will show the chosen `Layer` at top right of the Inspector. The example below has `User Layer 13` set to "Wall". This causes the assigned `GameObject` to be treated as part of a building.

In the following script example, [Physics.Raycast](/engine/6000.5/script-reference/unityengine/physics/raycast.md) sends a ray into the world. [Camera.main](/engine/6000.5/script-reference/unityengine/camera/main.md) can be rotated around the y-axis and fire a ray. Three [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md)s represent walls that can be hit by the fired ray. Each [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md) has [GameObject.layer](/engine/6000.5/script-reference/unityengine/gameobject/layer.md) set to the "Wall" layerMask.

```csharp
using UnityEngine;

// Fire a gun at 3 walls in the scene.
//
// The Raycast will be aimed in the range of -45 to 45 degrees. If the Ray hits any of the
// walls true will be returned . The three walls all have a Wall Layer attached.  The left
// and right keys, and the space key, are all used to aim and fire.
//
// Quad floor based at (0, -0.5, 0), rotated in x by 90 degrees, scale (8, 8, 8).
// ZCube wall at (0, 0.5, 6), scale (3, 2, 0.5).
// LCube wall at (-3, 0, 3), scale (0.5, 1, 4).
// RCube wall at (3, 1.5, 3), scale (1, 4, 4).

public class ExampleScript : MonoBehaviour
{
    private float cameraRotation;

    void Start()
    {
        Camera.main.transform.position = new Vector3(0, 0.5f, 0);
        cameraRotation = 0.0f;
    }

    // Rotate the camera based on what the user wants to look at.
    // Avoid rotating more than +/-45 degrees.
    void Update()
    {
        if (Input.GetKey("left"))
        {
            cameraRotation -= 1f;
            if (cameraRotation < -45.0f)
            {
                cameraRotation = -45.0f;
            }
        }

        if (Input.GetKey("right"))
        {
            cameraRotation += 1f;
            if (cameraRotation > 45.0f)
            {
                cameraRotation = 45.0f;
            }
        }

        // Rotate the camera
        Camera.main.transform.localEulerAngles = new Vector3(0.0f, cameraRotation, 0.0f);
    }

    void FixedUpdate()
    {
        Transform transform = Camera.main.transform;

        if (Input.GetKeyUp("space"))
        {
            // Check for a Wall.
            LayerMask mask = LayerMask.GetMask("Wall");

            // Check if a Wall is hit.
            if (Physics.Raycast(transform.position, transform.forward, 20.0f, mask))
            {
                Debug.Log("Fired and hit a wall");
            }
        }
    }
}
```

**Note:** [LayerMask](/engine/6000.5/script-reference/unityengine/layermask.md) is a bitmask.  Use [LayerMask.GetMask](/engine/6000.5/script-reference/unityengine/layermask/getmask.md) and [LayerMask.LayerToName](/engine/6000.5/script-reference/unityengine/layermask/layertoname.md) to generate the bitmask.

## Properties

| Property                                                                | Description                                      |
| ----------------------------------------------------------------------- | ------------------------------------------------ |
| [value](/engine/6000.5/script-reference/unityengine/layermask/value.md) | Converts a layer mask value to an integer value. |

## Static Methods

| Method                                                                              | Description                                                                                                                                                                                                                                                          |
| ----------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [GetMask](/engine/6000.5/script-reference/unityengine/layermask/getmask.md)         | Given a set of layer names as defined by either a Builtin or a User Layer in the [Tags and Layers manager](/engine/6000.5/manual/unity-editor/editor-settings-reference/comp-manager-group/class-tag-manager.md), returns the equivalent layer mask for all of them. |
| [LayerToName](/engine/6000.5/script-reference/unityengine/layermask/layertoname.md) | Given a layer number, returns the name of the layer as defined in either a Builtin or a User Layer in the [Tags and Layers manager](/engine/6000.5/manual/unity-editor/editor-settings-reference/comp-manager-group/class-tag-manager.md).                           |
| [NameToLayer](/engine/6000.5/script-reference/unityengine/layermask/nametolayer.md) | Given a layer name, returns the layer index as defined by either a Builtin or a User Layer in the [Tags and Layers manager](/engine/6000.5/manual/unity-editor/editor-settings-reference/comp-manager-group/class-tag-manager.md).                                   |

## Operators

| Operator                                                                          | Description                                    |
| --------------------------------------------------------------------------------- | ---------------------------------------------- |
| [LayerMask](/engine/6000.5/script-reference/unityengine/layermask/op-implicit.md) | Implicitly converts an integer to a LayerMask. |
