LayerMask
Specifies Layers to use in a Physics.Raycast.
Read time 3 minutesLast updated 5 days ago
Definition
- Type: Struct
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public struct LayerMask
Remarks
A GameObject can use up to 32 LayerMasks supported by the Editor. The first 8 of these are specified by Unity; the following 24 are controllable by the user.
LayersBitmasks represent the 32 Layers and define them as or . Each bitmask describes whether the is used. As an example, bit 5 can be set to 1 (). This will allow the use of the built-in setting.
truefalseLayertrueWaterEdit->Settings->Tags and LayersLayerBuilt-in Layer 0DefaultBuilt-in Layer 1TransparentFXLayerGameObjectLayerUser Layer 13GameObjectIn the following script example, Physics.Raycast sends a ray into the world. Camera.main can be rotated around the y-axis and fire a ray. Three GameObjects represent walls that can be hit by the fired ray. Each GameObject has GameObject.layer set to the "Wall" layerMask.
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 is a bitmask. Use LayerMask.GetMask and LayerMask.LayerToName to generate the bitmask.
Properties
Property | Description |
|---|---|
| value | Converts a layer mask value to an integer value. |
Methods
Method | Description |
|---|---|
| ToString | Given a layer mask, returns a comma seperated list of the names of the layers in this mask. The names are as defined in either a Builtin or a User Layer in the Tags and Layers manager. |
Static Methods
Method | Description |
|---|---|
| GetMask | Given a set of layer names as defined by either a Builtin or a User Layer in the Tags and Layers manager, returns the equivalent layer mask for all of them. |
| LayerToName | 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. |
| NameToLayer | Given a layer name, returns the layer index as defined by either a Builtin or a User Layer in the Tags and Layers manager. |
Operators
Operator | Description |
|---|---|
| LayerMask | Implicitly converts an integer to a LayerMask. |