Skip to main content

Layers

A layer is a name you put on an object so a camera, a raycast or the physics can treat a whole group alike: the minimap camera that leaves out the clouds, the shot that only hits enemies. Every object starts on Default.

Make layers​

  1. Project settings → Layers → + Add layer, name it (Clouds, Enemy, Water).
  2. Select an object: a Layer row appears under its name once the project has any layers. Pick the layer.

Renaming a layer in Project settings moves its objects and the cameras' choices with it. Deleting one puts its objects back on Default.

Leave layers out of a camera​

Every camera has a Draw switch for every layer (Unity's culling mask). Switch Draw Clouds off and that camera's picture shows no clouds. Only the object itself is left out: things attached to it, on other layers, still draw.

It works for the game's own camera as well as for cameras with a Target texture (see Render textures). That is how you make minimap icons: put a flat marker above each enemy on a Minimap layer, switch Draw Minimap off on the main camera and leave it on for the minimap camera. The player sees the enemies and the minimap shows the markers. The camera preview in the 3D view follows these switches too.

Look at only some layers with a raycast​

const hit = game.raycast(muzzle.position, { direction: aim, length: 50, layers: ['Enemy'] });

layers takes a name or a list; only objects on those layers can be hit. It combines with tags and ignore.

Choose which layers collide​

Open Project settings → Layer collisions. Every pair of layers (Default included) has a toggle: lit means the two collide, dark means objects on them pass through each other, and no collision or trigger event happens between them. This is Unity's Layer Collision Matrix. Everything collides until you switch a pair off.

  • Use it for ghosts that walk through walls, bullets that ignore the shooter's team, or pickups the player passes through while enemies cannot.
  • It follows an object when its layer changes while playing (Set layer, entity.layer = ...), so a player can turn into a ghost for a moment.
  • Raycasts have their own layer list (above) and ignore this matrix. A character's ground check and auto-step only see layers it collides with.
  • Renaming a layer keeps its switches; deleting a layer removes them. There is room for 14 layers besides Default.

From events and scripts​

  • Event block Set layer moves an object to a layer by name.
  • Script: entity.layer reads the layer name and can be set: crate.layer = 'Water'.
  • Event block Draw layer (Camera) makes a camera draw a layer, leave it out, or toggle it, from the next frame: Draw layer Minimap in MapCam after the player picks up the map. The condition Camera draws layer is true while its switch is on.
  • Script: cam.setDrawLayer('Minimap', true) draws the layer, false leaves it out; cam.drawsLayer('Minimap') says whether it is drawn. cam.component('camera').hideLayers = ['Minimap'] sets the whole list at once.

Good to know​

  • An object is on one layer. Use tags when it needs several labels.
  • The block and the script take any name, but only layers made in Project settings do anything for cameras; setting a name that does not exist reports it in the console (the block) and leaves the object on that name.
  • Layers collide with each other unless you switch a pair off in Layer collisions.