Skip to content

Tutorials · Physics and Mechanisms · Chapter 5

Crates

30 minutes Uses: CharacterBody, Collider, Sprite

By the end

  • Four collision layers named in Project settings, World, Knight, Crate and Grate, and one pair of them unticked in the collision matrix: Knight and Grate
  • The knight’s Character Body on Knight, beside his Area: what he collides as, and what plates notice, two fields
  • Three crates, each a Character Body that nothing drives but a shove, Pushable, with a solid Collider and an Area on Crate
  • A grate a crate stops at and the knight walks through, its rail he stands on by Include Layers, and a chute he stands on and crates fall through by Exclude Layers
  • The storeroom’s way up by keys: a crate on the plate holds the portcullis up, and two crates stacked lift him to the pillar under the way up
On this page

Four layers

Until now every Collider and body has been on layer 0, and everything has met everything. This chapter puts the crates on a layer of their own and a grate on another, which the knight passes through and a crate does not. A project names its collision layers once, for every scene, in Project settings.

  1. Choose File → Project settings… and open its Physics tab. Under Layer names it says that no layer has a name yet. Click Show all layers.
  2. Type World into the box of layer 0, Knight into 1, Crate into 2 and Grate into 3, pressing Enter after each.
  3. Under Collision matrix, find the row Grate and, in it, the column Knight. Untick that box; every other box stays ticked. Close the window.
The Project settings window on its Physics tab: Layer names, with a Show all layers button, and four rows, 0 World, 1 Knight, 2 Crate, 3 Grate; then Collision matrix and its line, Ticked pairs collide, untick a pair to let those layers pass through each other; a triangle of boxes with the columns World, Knight, Crate and Grate written upward and the rows World, Knight, Crate and Grate, every box ticked but one, in the row Grate under the column Knight; at the foot, the line Written to project.rengy as you change it, a scene stores only a layer’s number, so a name here changes nothing about what collides
The Physics tab as the chapter leaves it: four layers, and the knight passes through the grate.
Layer names
The Physics tab’s Layer names: a name for each collision layer the project uses, by its number. Every layer row in the Inspector shows these names from now on, and a layer joins the matrix below once it has one. A name is only a name: a scene keeps which layers are ticked, not the words, so renaming a layer changes nothing that collides.
Collision matrix
The Physics tab’s Collision matrix: one box for each pair of named layers. Ticked, a body on one of the two meets a Collider on the other; unticked, they pass through each other. Every box comes ticked. Knight with Grate unticked: the knight walks through the grate, and a crate, on Crate, whose box with Grate is ticked, does not. Areas look through it too: the plate, on World, notices the knight and the crates because World’s boxes with Knight and with Crate are ticked.

The knight’s Area went on layer 1 in chapter 4, which is now Knight. His body is still on World.

  1. Select knight. At the foot of his Character Body’s rows, click the button beside Layer, which reads World. Untick World and tick Knight, and click away from the list.
The Inspector narrowed with its search box, layer, to the knight’s layer rows: under the AreaComponent’s Shape the dim rows Layer 1, Include Layers 0 and Exclude Layers 0, then the Area’s Layer Knight and Mask Nothing; under the CharacterBodyComponent, Layer Knight
The knight’s two Layers, both Knight: his Area’s, which plates notice, and his body’s, which he collides as. The dim rows are his Area’s Shape’s, which chapter 4 says an Area never reads.
Layer
The Character Body’s Layer, at the foot of its rows: the layers the body is on, what it collides as. Moving, it meets each Collider whose layer has a ticked box with one of its own in the matrix, unless that Collider’s Include or Exclude Layers say otherwise. The knight’s is Knight; a new body’s is World. His Area’s Layer is another field, what a plate looks for; he needs both on Knight, and each is set on its own.

Three crates

A crate is a body the knight shoves: a Character Body ticked Pushable, with nothing to drive it. Walked into, it goes along at a share of his walk; let go, it stops; off an edge, it falls; on another crate, it rides it. It never turns, never tips, and is never pulled. It needs a solid Collider of its own to be walked into, and an Area so that the plate can notice it.

  1. Select storeroom, click + Add node and choose Sprite. Rename it crate_1 and set its Position to 460, 1568, between the plate and the scaffolding.
  2. In its Sprite, drag art/crate.png from Assets onto Texture, and set Size to 24, 24, Anchor to 0.5, 1 and Sort Layer to 2: hung from its feet, on the floor.
  3. Click Add component and choose CharacterBodyComponent. Set its Size to 24, 24 and Driven By to Script. In its Pushing fold, tick Pushable and set Push Speed to 0.75. At the foot of its rows, click the button beside Layer, untick World and tick Crate.
  4. Click Add component and choose ColliderComponent. Set its Size to 24, 24 and Offset to 0, -12: the crate’s box, standing on its feet. Solid stays ticked. Click the button beside its Layer, untick World and tick Crate.
  5. Click Add component and choose AreaComponent. Open its Shape and set Size to 24, 24 and Offset to 0, -12. Click the button beside the Area’s own Layer, untick World and tick Crate. Click the one beside Mask and untick World, so that it reads Nothing.
  6. In the Hierarchy, right-click crate_1 and choose Duplicate. Rename the copy crate_2 and set its Position to 157, 1568, on the floor west of the doorway. Duplicate it again: crate_3, at 224, 1448. It hangs in the air until the shelf is built under it, below.
The Inspector narrowed with its search box, character, to crate_1’s CharacterBodyComponent, every row: Size 24.0 and 24.0, Motion Mode Grounded, Driven By Script, Velocity 0.0 and 0.0, Drift 0.0 and 0.0 in dim type, Gravity 1800.00, Terminal Velocity 900.00, Speed 220.00, Jump Speed 620.00, the Jumping and Slopes folds as a new body has them, the Pushing fold with Pushable ticked and Push Speed 0.75, Grounded, On Wall, Shoving, On Ceiling and Drop Through unticked, Layer Crate, Debug Color green
crate_1’s Character Body: driven by Script, with no script; Pushable, at a Push Speed of 0.75; on Crate.
The Inspector narrowed with its search box, layer, to crate_1’s layer rows: under the AreaComponent’s Shape the dim rows Layer 1, Include Layers 0 and Exclude Layers 0, then the Area’s Layer Crate and Mask Nothing; the CharacterBodyComponent’s Layer Crate; the ColliderComponent’s Layer Crate, Include Layers Nothing, Exclude Layers Nothing
crate_1’s three Layers, all Crate: its body’s, its Collider’s and its Area’s.
Driven By
The Character Body’s Driven By: who sets the body’s own velocity. Keys, as the knight’s, walks it with A and D and jumps it with Space; Script leaves it to a script. A crate is Script with no script, so nothing ever walks it: the keys never do, and only a shove, gravity and what the shove leaves it move it. Left at Keys, it would walk beside him. A new body’s is Keys.
Pushable
The Character Body’s Pushable, in its Pushing fold: ticked, a body that walks into this one shoves it along, instead of stopping. It needs a solid Collider of its own to be walked into, as each crate has. A new body’s is unticked: the knight is not pushable.
Push Speed
The Character Body’s Push Speed: how much of the pusher’s walk a shove gives this body, from 0 to 1. At 1 it keeps pace with him; at 0.5, as a new body comes, it goes at half his speed, and he with it. The crates’ 0.75 takes his walk of 160 a second to 120 while he shoves: he is slowed as by a weight, not stopped.
Layer
The Collider’s Layer: the layers it is on, what it is to everything that meets it, looks for it or casts at it. A crate’s Collider is on Crate, as its body is: its body is what moves it and what it collides as, its Collider what the knight walks into and what another crate lands on. A new Collider is on World, as the tower’s stone is.

Every other field of a crate’s body is left as it comes. Nothing drives a crate, so its Speed and Jump Speed are never used; its Gravity, 1800, still pulls it down.

A grate he walks through

At the doorway’s west mouth, a grate stops crates, so none is shoved under the portcullis, and lets the knight through. It is a Collider on Grate: a crate, on Crate, meets it, and the knight, on Knight, whose box with Grate is the one unticked, passes. Its top rail is his step up to the shelf, so the rail must hold him, though it is a part of the grate.

  1. Select storeroom and add a Sprite named grate: Position 248, 1568; Texture grate.png, Size 16, 40, Anchor 0.5, 1, Sort Layer 1.
  2. Click Add component and choose ColliderComponent: Size 16, 40, Offset 0, -20. Click the button beside its Layer, untick World and tick Grate.
  3. Select grate, click + Add node and choose Node. Rename it grate_rail and set its Position to 0, -40.
  4. Click Add component and choose ColliderComponent: Size 16, 3, Offset 0, 1.5. Click the button beside its Layer, untick World and tick Grate, like the grate. Click the button beside Include Layers and tick Knight.
The Inspector narrowed with its search box, layer, to grate_rail’s ColliderComponent: Layer Grate, Include Layers Knight, Exclude Layers Nothing
The rail: on Grate, like the grate, and including Knight.

A child’s Position is from its parent: grate_rail at 0, -40 is at the top of the grate, 40 over the floor, and its box, 3 tall, lies over the picture’s top rail.

Include Layers
The Collider’s Include Layers: layers that meet this Collider whatever the matrix says. A body on one of them stands on it and is stopped by it. It does not put the Collider on them: the rail stays on Grate, a part of the grate to everything else. So the knight, whom the matrix passes through Grate, stands on the rail and still walks under it, through the bars. A new Collider’s is Nothing.

A shelf, a chute and a pillar

West of the grate is the way up: a shelf over the floor with crate_3 on it, a chute beside the shelf that a crate falls through, and a pillar under the opening in the ceiling to the ice hall.

  1. Select storeroom and add a Sprite named shelf: Position 232, 1452; Texture scaffold-plank.png, Size 48, 8, Draw Mode Tiled and Tile Size 48, 8. Add a ColliderComponent: Size 48, 8, and tick One Way.
  2. Add another Sprite under storeroom, named chute: Position 192, 1452; Texture scaffold-plank.png, Size 32, 8, Draw Mode Tiled and Tile Size 48, 8. Add a ColliderComponent: Size 32, 8. Its Layer stays World. Click the button beside Exclude Layers and tick Crate.
  3. Add a third, storeroom_pillar: Position 32, 1572; Texture old-dark-castle-interior-tileset.png with Atlas Region pillar, Size 32, 144, Anchor 0.5, 1, Sort Layer -1, Draw Mode Tiled and Tile Size 32, 72: the tileset’s pillar twice, one on the other. Add a ColliderComponent: Size 32, 144, Offset 0, -72.
The Inspector narrowed with its search box, layer, to chute’s ColliderComponent: Layer World, Include Layers Nothing, Exclude Layers Crate
The chute: on World, as a Collider comes, and excluding Crate.

The shelf’s top is 120 over the floor: out of his reach from the floor, where his held jump lifts him 110, and in it from the rail, 40 up. The chute is a plank at the same height, so he stands on it, and a crate falls through it. The pillar stands 140 over the floor, its foot 4 into it behind the moss caps: two crates, 48, and his held jump clear its top by 18, and one crate leaves him 6 short. From its top he jumps up through the opening onto the ice hall’s floor.

Exclude Layers
The Collider’s Exclude Layers: layers that pass through this Collider whatever the matrix says. It does not take the Collider off its own Layer: the chute stays on World, so the knight stands on it, and a crate, on Crate, falls through it. Include and Exclude Layers count from either of two Colliders that meet, and where one includes a layer and the other excludes it, the exclude wins: they pass. A new Collider’s is Nothing.

The plate looks for crates

  1. Select plate. Click the button beside its Area’s Mask, which reads Knight, and tick Crate as well: it reads Knight, Crate. Press Ctrl+S.
Part of the Inspector: the plate’s Area, its Layer row reading World and its Mask row reading Knight, Crate, and under it the list open: World unticked, Knight ticked, Crate ticked, Grate unticked, and boxes 4 to 7 unticked, then the fold Name layers…
The plate’s Mask, its list open: the layers by name now.

plate.rgs does not change: it counts whatever its Area notices, a crate as well as the knight. The storeroom is built.

The storeroom in the editor, its backdrop selected and framed at 128.3%, with gizmos: at the west wall the pillar, two tiles of stone stacked, with the opening in the ceiling above it; crate_2 on the floor; the shelf with crate_3 on it and the short chute plank at its west end; the grate, narrow and barred, its rail at its top, and beside it the portcullis down in the doorway under the hanging wall; the plate in the floor; crate_1 between the plate and the scaffolding’s top plank at the east end; each crate outlined in rose
The storeroom built, at rest: the pillar, crate_2, the shelf with crate_3, the chute, the grate and its rail, the portcullis, the plate and crate_1.

Shoving

  1. If you take chapter 4’s skip: with the game stopped, select knight and set his Position to 560, 1568, on the scaffolding’s top plank.
  2. Select knight, so that the Inspector shows his Character Body while the game runs, and press F5.
  3. Walk him up the scaffolding into the storeroom, and hold A: he walks into crate_1 and shoves it west, slower than he walks. In the Inspector, Shoving is ticked on every step he shoves.
  4. Let go of A as the crate reaches the plate. The portcullis rises, and stays up with him off the plate.
Three frames of the floor from the doorway to east of the plate, close up, in Play with gizmos, the plate outlined in blue as the selected node: the knight shoving crate_1 west, a rose arrow from his box pointing west and an orange arrow under the crate’s feet pointing west, the portcullis down in the doorway at the left; the crate on the plate, the portcullis still down; one step later, the portcullis up in the wall, only its spikes showing
Steps 55, 74 and 75 (0.92, 1.23 and 1.25 s), the editor in Play at 200%, framed on the plate, with the Collision, Triggers and Bodies gizmos, A held from step 0 to 74: he meets the crate on step 31. On step 74 the plate counts the crate, and on 75 the portcullis is up. The orange arrow under the crate is its Drift, the shove. For this picture he was stood on the scaffolding’s top plank from the start, at the skip’s 560, 1568.
The editor in Play at step 55, 0.9 s: the knight selected and framed at 1243.7%, his cloak flying, his box against crate_1’s at the left and a rose arrow from his box pointing west; the Inspector narrowed with its search box, character, to his Character Body: Velocity -160.0 and 0.0, Drift 0.0 and 0.0, Gravity 2000.00, Speed 160.00, Jump Speed 680.00, the Pushing fold with Pushable unticked, Grounded ticked, On Wall unticked, Shoving ticked, Layer Knight
Step 55 of the same run (0.92 s): Shoving ticked, live. His Velocity reads -160, his walk; the crate holds him to its 120.
Shoving
The Character Body’s Shoving, read live in Play: ticked on a step this body shoved a Pushable body along. It is off when he walks free, and off when what he walks into will not give way: then he is On Wall instead. The body writes it every step, so there is nothing to type.
On Wall
The Character Body’s On Wall, read live, as in chapter 2: ticked on a step his walk was stopped sideways. A crate that cannot move, against the grate or the pillar, stops him as a wall does, and he is On Wall, not Shoving.
Four frames of the floor around the plate, close up, an arched window behind, the plate outlined in blue as the selected node: the knight with his box against crate_1’s, the crate just over the plate’s east end, a long rose arrow from his box pointing west and a short orange one under the crate’s feet; then the crate a little further west, a gap opening between it and him, the knight drawn faint as his clip changes; then further, its orange arrow shorter; and in the last the crate at rest further over the plate, him standing where he let go
Steps 74, 78, 84 and 92 of the same run (1.23, 1.30, 1.40 and 1.53 s), framed on the plate as the strip above: A is let go of on step 74, as the plate counts the crate. The crate goes on from x 372 to 367, 364.2 and 363.2, and rests at 363 by step 94; the orange Drift arrow under its feet shortens as it goes. He stands where he let go, at 392.
Four frames of the floor from the portcullis to east of the plate, close up, the plate outlined in blue: the knight shoving crate_1 west, both over the plate, the portcullis drawn up into the wall with only its spiked foot under the arch and its green outline still standing in the open doorway; the crate past the plate’s west end, the knight on the plate behind it, the portcullis still up; both west of the plate, the portcullis down in the doorway, its bars from the floor to the arch; and the crate against the portcullis, the knight behind it, both stopped
Steps 90, 98, 104 and 112 of a run with A held from the start (1.50, 1.63, 1.73 and 1.87 s), framed on the plate: both on the plate, the portcullis up; the crate off its far side, him still on it; both off it, and the portcullis down on step 104; from step 110 the crate against it, at x 300, and he behind it. For this picture he was stood on the scaffolding’s top plank from the start, at the skip’s 560, 1568.

The way up

  1. With the crate on the plate, hop over it: tap Space with A held. Walk on west, under the raised portcullis and through the grate.
Three frames of the floor from west of the grate to the plate, close up: the knight in the air over crate_1, which rests on the plate, the portcullis up; the knight walking under the raised portcullis, a rose arrow pointing west; the knight west of the grate, its bars and rail behind him
The same run, framed on the plate: steps 105, 140 and 165 (1.75, 2.33 and 2.75 s). A from step 94 with Space from 94 to 97, a short hop; the portcullis stays up on every step, the crate holding the plate down.

A crate does not get through. Here crate_2 is shoved east, into the grate: it stops at the grate’s west face, and the knight behind it is stopped too.

Four frames of the west floor up to the grate, close up, the grate outlined in blue as the selected node: the knight standing west of crate_2; shoving it east, a rose arrow pointing east from his box across the crate and an orange one under the crate; the crate against the grate’s west face; the knight still behind it, the crate not moving
Steps 0, 20, 39 and 50 (0.00, 0.33, 0.65 and 0.83 s), the editor in Play at 200%, framed on the grate: D held from step 0 to 60. On step 39 the crate meets the grate. For this picture he was stood west of crate_2 from the start.
The editor in Play at step 50, 0.8 s: the knight selected and framed at 1262.2%, his box against crate_2’s and the grate’s bars behind the crate; the Inspector narrowed with its search box, character, to his Character Body: Velocity 0.0 and 0.0, Drift 0.0 and 0.0, Grounded ticked, On Wall ticked, Shoving unticked, Layer Knight
Step 50 of the same run (0.83 s): On Wall, not Shoving. The crate will not give way, so to him it is a wall.

You need not do that yourself: crate_2 has to stay where it is, under the chute. If you do, press Stop and Play and climb back.

  1. From the floor just west of the grate, jump back east onto its rail: hold D, press Space, and let go of D as he comes over it. From the rail, jump up through the shelf onto it.
  2. On the shelf, walk west into crate_3, and keep walking: it goes off the shelf’s west end, falls through the chute onto crate_2, and stands on it. Walk on onto the chute and stop there.
Four frames of the shelf and the floor under it, close up, crate_2 on the floor below: the knight shoving crate_3 west along the shelf; the crate falling through the chute’s plank at the shelf’s west end, a rose arrow pointing down from it and an orange one pointing west under it, the knight at the shelf’s end; the crate below the chute, still falling, its rose arrow reaching the floor; crate_3 standing on crate_2, and the knight standing on the chute’s plank
Steps 10, 22, 30 and 40 (0.17, 0.37, 0.50 and 0.67 s), the editor in Play at 200%, framed on the pillar: A held from step 0 to 23. The crate leaves the shelf on step 16 and lands on crate_2 on step 35. For this picture he was stood on the shelf, east of crate_3, from the start.
  1. Walk back east onto the shelf and drop through it: S with Space. He lands on the floor east of the stack.
  2. Hold A: he shoves the stack west, the top crate riding the bottom one, until the pillar stops it.
  3. Jump onto the stack. From it, jump onto the pillar, holding A. From the pillar, jump with D held, up through the opening onto the ice hall’s floor.
  4. Press F5 to stop. If you took the skip, select knight and set his Position back to 48, 1792, his start.
Four frames of the storeroom’s west end, close up, the pillar outlined in blue as the selected node: the knight shoving the two stacked crates west, the top one riding the bottom one; the stack against the pillar, him behind it; the knight standing on the stack against the pillar’s side; the knight on the pillar’s top
The same run as the chute’s, framed on the pillar: steps 130, 157, 200 and 235 (2.17, 2.62, 3.33 and 3.92 s). D from step 53 to 62 and S with Space on 62 drop him through the shelf; A from 90 to 161 shoves the stack, which the pillar stops on 155; A with Space from 165 to 200, and from 203 to 236, jump him onto the stack and the pillar.
Four small frames of the pillar’s top and the opening over it, the ice hall’s floor east of the opening: the knight on the pillar’s top; rising through the opening, a long rose arrow pointing up and east; at the top of the jump over the ice hall’s floor, his arrow pointing east; landed on the ice hall’s floor east of the opening
The same run: steps 239, 248, 258 and 271 (3.98, 4.13, 4.30 and 4.52 s), D with Space from 239 to 271. Framed on the pillar at 100%: at 200% the opening is above the view.

The chute is no way round the stack: under the ice hall’s slab, a jump from it rises 42 at most before his head meets the slab, and it comes down short of the pillar.

The layers, the crates and the grate

WhereFieldValue
Project settings, PhysicsLayer names0 World, 1 Knight, 2 Crate, 3 Grate
Collision matrixevery box ticked but Knight with Grate
NodeComponentFieldValue
knightCharacter BodyLayerKnight
plateAreaMaskKnight, Crate
crate_1, crate_2, crate_3Position460, 1568; 157, 1568; 224, 1448
SpriteTexture, Sizecrate.png, 24, 24
Anchor, Sort Layer0.5, 1; 2
Character BodySize24, 24
Driven ByScript
Pushable, Push Speedticked, 0.75
LayerCrate
ColliderSize, Offset24, 24; 0, -12
LayerCrate
AreaShape › Size, Offset24, 24; 0, -12
LayerCrate
MaskNothing
NodeComponentFieldValue
gratePosition248, 1568
SpriteTexture, Sizegrate.png, 16, 40
Anchor, Sort Layer0.5, 1; 1
ColliderSize, Offset16, 40; 0, -20
LayerGrate
grate_rail, under gratePosition0, -40
ColliderSize, Offset16, 3; 0, 1.5
LayerGrate
Include LayersKnight
shelfPosition232, 1452
SpriteTexture, Sizescaffold-plank.png, 48, 8
Draw Mode, Tile SizeTiled, 48, 8
ColliderSize48, 8
One Wayticked
chutePosition192, 1452
SpriteTexture, Sizescaffold-plank.png, 32, 8
Draw Mode, Tile SizeTiled, 48, 8
ColliderSize32, 8
LayerWorld
Exclude LayersCrate
storeroom_pillarPosition32, 1572
SpriteTexture, Atlas Regionthe tileset, pillar
Size, Anchor32, 144; 0.5, 1
Sort Layer-1
Draw Mode, Tile SizeTiled, 32, 72
ColliderSize, Offset32, 144; 0, -72

Every other field is as it comes. Every node here is under storeroom but the rail, which is under grate.

Check it works