# PhysicsMask

> A 64-bit mask, effectively 64 flags. The default enumerator will iterate all the bits that are set (1).

## Definition

* **Type:** Struct
* **Namespace:** [Unity.U2D.Physics](/engine/6000.7/script-reference/unity/u2d/physics.md)
* **Assembly:** UnityEngine.PhysicsCore2DModule
* **Implements:** [IEnumerable\<int>](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1), [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.ienumerable), [IEquatable\<PhysicsMask>](https://learn.microsoft.com/dotnet/api/system.iequatable-1)

```csharp
public struct PhysicsMask : IEnumerable<int>, IEnumerable, IEquatable<PhysicsMask>
```

## Remarks

Use a `PhysicsMask` together with [PhysicsLayers.GetLayerMask](/engine/6000.7/script-reference/unity/u2d/physics/physicslayers/getlayermask.md) and [PhysicsShape.ContactFilter](/engine/6000.7/script-reference/unity/u2d/physics/physicsshape/contactfilter.md) to control which objects collide with each other in a script. If the field or property that stores a `PhysicsMask` represents a raw bitmask rather than a set of named layers, apply [PhysicsMask.ShowAsPhysicsMaskAttribute](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/showasphysicsmaskattribute.md) so Unity displays it as bit values in the Inspector window.

Additional Resources: [PhysicsLayers.GetLayerMask](/engine/6000.7/script-reference/unity/u2d/physics/physicslayers/getlayermask.md), [PhysicsShape.ContactFilter](/engine/6000.7/script-reference/unity/u2d/physics/physicsshape/contactfilter.md)

## Examples

```csharp
// Get the physics mask for the "Car" layer, then get another mask for the
// layers the object should collide with.
using UnityEngine;
using Unity.U2D.Physics;

public class PhysicsMaskExample : MonoBehaviour
{
    void Start()
    {
        PhysicsMask objectLayer = PhysicsLayers.GetLayerMask("Car");
        PhysicsMask contactLayer = PhysicsLayers.GetLayerMask("Walls");
        PhysicsShape.ContactFilter myContactFilter = new PhysicsShape.ContactFilter
        {
            categories = objectLayer,
            contacts = contactLayer
        };
    }
}
```

## Fields

| Value                                                                               | Description                          |
| ----------------------------------------------------------------------------------- | ------------------------------------ |
| [bitMask](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/bitmask.md) | A 64-bit mask, effectively 64 flags. |

## Static Fields

| Value                                                                         | Description                                                          |
| ----------------------------------------------------------------------------- | -------------------------------------------------------------------- |
| [All](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/all.md)   | All 64 bits set (1) in the PhysicsMask.                              |
| [None](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/none.md) | No bits set in the PhysicsMask, effectively zero.                    |
| [One](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/one.md)   | Bit #0 set (1) in the PhysicsMask. The remaining bits are reset (0). |

## Constructors

| Constructor                                                                                                                          | Description                                                                                                                                                                                     |
| ------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [PhysicsMask(System.Int32\[\])](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/ctor.md#physicsmask\(int\))            | Create a PhysicsMask by specifying multiple bits to set (1).                                                                                                                                    |
| [PhysicsMask(UnityEngine.LayerMask)](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/ctor.md#physicsmask\(layermask\)) | Create a PhysicsMask from a LayerMask. A [LayerMask](/engine/6000.7/script-reference/unityengine/layermask.md) is only 32-bits wide so the PhysicsMask will have the upper 32-bits set to zero. |

## Properties

| Property                                                                                | Description                                                                                                                                                                                                                                     |
| --------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [resetBits](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/resetbits.md) | Gets an enumerable group of bits that are currently reset (0). The bits are returned in ascending bit-index order. This uses [PhysicsMask.ResetBitIterator](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/resetbititerator.md). |
| [setBits](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/setbits.md)     | Gets an enumerable group of bits that are currently set (1). The bits are returned in ascending bit-index order. This uses [PhysicsMask.SetBitIterator](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/setbititerator.md).       |

## Methods

| Method                                                                                          | Description                                                                                                                                                                                                                                                                             |
| ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AreAnyBitsSet](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/areanybitsset.md) | Checks if any of the provided PhysicsMask set bits are also set in this PhysicsMask.                                                                                                                                                                                                    |
| [AreBitsSet](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/arebitsset.md)       | Checks if all the provided PhysicsMask set bits are also set in this PhysicsMask.                                                                                                                                                                                                       |
| [IsBitSet](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/isbitset.md)           | Is the specified bit set.                                                                                                                                                                                                                                                               |
| [ResetBit](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/resetbit.md)           | Reset (0) the specified bit.                                                                                                                                                                                                                                                            |
| [SetBit](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/setbit.md)               | Set (1) the specified bit.                                                                                                                                                                                                                                                              |
| [ToLayerMask](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/tolayermask.md)     | Convert the lower 32-bits of the 64-bit mask to the 32-bit [LayerMask](/engine/6000.7/script-reference/unityengine/layermask.md). A [LayerMask](/engine/6000.7/script-reference/unityengine/layermask.md) is only 32-bits wide so the upper 32-bits of the PhysicsMask will be ignored. |

## Operators

| Operator                                                                                          | Description                                   |
| ------------------------------------------------------------------------------------------------- | --------------------------------------------- |
| [operator &](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/op-bitwiseand.md)      | Bitwise AND operator for PhysicsMask.         |
| [operator \|](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/op-bitwiseor.md)      | Bitwise OR operator for PhysicsMask.          |
| [operator ^](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/op-exclusiveor.md)     | Bitwise XOR operator for PhysicsMask.         |
| [operator \<\<](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/op-leftshift.md)    | Bitwise LEFT-SHIFT operator for PhysicsMask.  |
| [operator \~](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/op-onescomplement.md) | Bitwise COMPLEMENT operator for PhysicsMask.  |
| [operator >>](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/op-rightshift.md)     | Bitwise RIGHT-SHIFT operator for PhysicsMask. |

## Classes

| Class                                                                                                                                 | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [PhysicsMask.ShowAsPhysicsMaskAttribute](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/showasphysicsmaskattribute.md) | When applied to a field/property of type [PhysicsMask](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask.md), the field/property drawer will not be display it as [PhysicsLayers](/engine/6000.7/script-reference/unity/u2d/physics/physicslayers.md). Instead, the field/property will be displayed as bit numbers only i.e. a raw 64-bit mask allowing each bit to be (de)selected. This is only used when physics layers are active (see [\_usePhysicsLayers](/engine/6000.7/script-reference/unity/u2d/physics/physicscoresettings2d/usephysicslayers.md)). |

## Structs

| Struct                                                                                                            | Description                                                                                                                                         |
| ----------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| [PhysicsMask.ResetBitIterator](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/resetbititerator.md) | An iterator that will iterate only the bits that are reset (0) in a [PhysicsMask](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask.md) |
| [PhysicsMask.SetBitIterator](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask/setbititerator.md)     | An iterator that will iterate only the bits that are set (1) in a [PhysicsMask](/engine/6000.7/script-reference/unity/u2d/physics/physicsmask.md)   |
