Physics layers
Updated
By default every collider collides with every other one. Real projects need exceptions: ghosts that float through walls, bullets that pass through their own shooter, glass the camera can see through, a player who should not trip over the leaves on the ground. Physics layers express these exceptions. Each collider sits on one layer, and the project’s collision matrix says how each pair of layers meets.
Physics layers are separate from the Layers that group entities for rendering, picking and switching off.
Before you start
Section titled “Before you start”- You have entities with colliders. See Rigid bodies and colliders.
- You know the difference between a collision and an intersection. See Collisions and triggers.
Layers
Section titled “Layers”A project has at most 16 physics layers, numbered by id from 0 to 15. Each has:
| Field | Description |
|---|---|
id |
0 to 15. This is the number colliders and scripts use. |
name |
The name the editor shows. |
color |
The colour the editor shows. |
Layer 0, Default, always exists, and every collider starts on it. It can be renamed and recoloured, but not removed. You define the other layers, and the matrix, in the project’s physics settings in the editor.
A collider that names a layer the project does not define is built on Default.
The collision matrix
Section titled “The collision matrix”Each pair of layers meets in one of three ways. A layer is also paired with itself.
| Interaction | What happens | Events |
|---|---|---|
collide |
The colliders push each other. | Physics and collision- |
intersect |
They pass through each other, but the overlap is reported. | Physics and intersection- |
ignore |
They never meet. Nothing is reported. | none |
- Every pair collides until set otherwise.
- The matrix is symmetric: how A meets B is how B meets A.
- A sensor never pushes. It intersects every layer its own layer collides or intersects with, and meets nothing on an
ignorepair. - A layer that is removed and added again collides with every layer.
An example set of layers, used by the examples below:
| id | Name | How it meets the other layers |
|---|---|---|
| 0 | Default | Collides with every layer. |
| 1 | Player | Collides with every layer, except Thrown, which it intersects. |
| 2 | Ghost | Ignores Walls; collides with every other layer. |
| 3 | Walls | Ignores Ghost; collides with every other layer. |
| 4 | Glass | Collides with every layer. Sight rays leave it out. |
| 5, 6 | Props, Tools | Collide with every layer. |
| 7 | Thrown | Intersects Player; collides with every other layer. |
Your own matrix will differ; scripts only need the ids.
Who reads the layers
Section titled “Who reads the layers”| Who | Prop or option | Effect |
|---|---|---|
| Every collider | collision |
The layer the collider is on. |
| Character Controller | collision |
The layer its capsule is on. Only colliders on layers it collides with stop it. |
| Camera Collision | collision |
The layer the camera’s sweep is on. Only layers it collides with pull the camera in; layers it intersects or ignores, such as glass or foliage, let it through. |
Physics |
collision option |
The ray hits only colliders on those layers. Absent, every layer; an empty list, none. |
Every collisionLayer is a whole number from 0 to 15. Change it from a script with setProps({ collisionLayer: id }); the body is rebuilt on the new layer.
Examples
Section titled “Examples”The examples use the layer ids from the example matrix above. Name your layer ids once, at the top of a script, so a reader knows what 3 means.
1. Ghost mode
Section titled “1. Ghost mode”While a power-up lasts, the player walks through walls.
#pragma lifecycle(startup)
const PLAYER_LAYER = 1;const GHOST_LAYER = 2; // ignores the Walls layer in the project's matrixconst DURATION = 5000; // milliseconds
let character;let cancelTimer = null;
function startup() { character = self.parent.getComponent(CharacterControllerComponent);
on('powerup/ghost', () => { character.setProps({ collisionLayer: GHOST_LAYER }); log('Ghost mode on'); if (cancelTimer) cancelTimer(); cancelTimer = delay(() => { character.setProps({ collisionLayer: PLAYER_LAYER }); log('Ghost mode off'); }, DURATION); });}A power-up moves the player's Character Controller to a Ghost physics layer that ignores the Walls layer for five seconds, so the player walks through walls.
Walk with WASD into a wall, then press Ghost mode and walk through it within five seconds.
- The Character Controller’s capsule sits on
collisionLayer. Moving it to the Ghost layer, which ignores Walls, lets it pass through walls while still standing on floors on other layers. delayswitches it back after five seconds. It returns a cancel function, kept so a second power-up restarts the timer instead of ending ghost mode early.- The layer ids come from the project’s physics settings; the constants document them.
2. Line of sight through glass
Section titled “2. Line of sight through glass”A guard sees the player through windows on the Glass layer, but not through walls.
#pragma lifecycle(startup, update)
const BLOCKS_SIGHT = [0, 3]; // Default and Walls; Glass (4) is left outconst eye = new THREE.Vector3();const target = new THREE.Vector3();
let guard;let player;let seen = false;
function startup() { guard = findEntity('Guard'); player = findEntity('Player');}
function update() { guard.getWorldPosition(eye); eye.y += 1.6; player.getWorldPosition(target); target.y += 1.6;
const blocked = PhysicsRaycaster.between(eye, target) .hit(null, { collisionLayers: BLOCKS_SIGHT, ignore: [guard, player] }); const nowSeen = !blocked; if (nowSeen !== seen) { seen = nowSeen; emit(seen ? 'guard/alert' : 'guard/lost'); }}A guard’s sight ray tests only the Default and Walls layers, so it is blocked by the wall but sees through the window on the Glass layer; an Enable On Signal shows when it spots the player.
Drag the blue player behind the grey wall to hide it, or behind the glass, where the guard still sees it and the pink alarm lights. Drag elsewhere to orbit the camera.
between(eye, target)casts a ray from the guard’s eyes to the player’s head that stops at the player.- Only colliders on the layers in
BLOCKS_SIGHTblock it. Glass is on layer 4, which is not listed, so the ray passes through windows. ignoreskips the guard’s and the player’s own colliders.seenremembers the last result, so the alert is emitted only when it changes, not every frame.
Snippets
Section titled “Snippets”1. Click only what is pickable
Section titled “1. Click only what is pickable”A ray from the mouse hits only colliders on the “pickable” layers, ignoring walls and floors.
#pragma lifecycle(update)
const PICKABLE = [5, 6]; // Props and Tools layers
function update() { if (!Input.mouse.isButtonReleased(MouseButton.Left)) return; const hit = PhysicsRaycaster.fromScreen(Input.mouse.position()).hit(null, { collisionLayers: PICKABLE }); if (hit) emit('picked', { id: hit.entity.entityId });}PhysicsRaycaster.fromScreenmakes a ray from the camera through the mouse position.collisionLayers: PICKABLElimits the ray to colliders on layers 5 and 6. A wall in front of a prop on another layer is passed through.PICKABLEis a constant outsideupdate, so no new array is built every frame.- See Picking and raycasts for every raycaster option.
2. Let thrown items pass through their thrower
Section titled “2. Let thrown items pass through their thrower”A ball thrown by the player starts inside the player’s capsule. For a moment it is put on a layer that intersects the Player layer, then returned to normal.
#pragma lifecycle(startup)
const THROWN_LAYER = 7; // intersects Player in the project's matrixconst NORMAL_LAYER = 0;
// On a dynamic Ball with a Rigid Body and a Sphere Collider.function startup() { const body = self.parent.getComponent(PhysicsRigidBodyComponent); const shape = self.parent.getComponent(PhysicsColliderComponent);
on('player/throw', (data) => { shape.setProps({ collisionLayer: THROWN_LAYER }); delay(() => { body.applyImpulse(data.impulse); }, 0); delay(() => shape.setProps({ collisionLayer: NORMAL_LAYER }), 400); });}getComponent(PhysicsColliderComponent)finds the ball’s collider whatever its shape.- Changing
collisionLayerrebuilds the ball’s body on the Thrown layer, which only intersects the Player layer, so the ball slips out of the capsule instead of being shoved back. - The impulse is applied on the next frame, after the layer change has been applied.
data.impulseis sent by the script that emitsplayer/throw. - After 400 milliseconds the ball returns to Default and collides with everything again.
Common mistakes
Section titled “Common mistakes”- Expecting layers to hide things. Physics layers only affect physics: contacts, triggers, the character, camera collision and physics rays. Use rendering layers to hide entities.
- Using a layer the project does not define. The collider is built on Default. Check the layer list in the project’s physics settings.
- Forgetting the matrix is symmetric. If Ghost ignores Walls, Walls ignore Ghost, for every collider on them.
- Building
collisionLayersarrays every frame. Keep them in constants. - An empty
collisionLayerslist. It hits nothing. Leave the option out to hit every layer. - Changing layers every frame. Each change rebuilds the body; do it on events.