◀ 7.17.27.3 ▶

7.2 — Tile Collision, Layers & Sorting

Your cave is already solid, you did not make it solid, and until today you could not have explained why. Three components work quietly under your feet, and the list of layers your own code from 6.2 gambles on was never finished. Today you find out what you inherited, and finish what you started.

In this lesson you will be able to answer“why is my tilemap solid, and why is my player in front of it?”Outline only
Break It Down~10 minOutline only

The cave floor already holds you up and the player already draws in front of the backdrop, and you wrote neither of those facts. So the job is not "make a tilemap solid" — it is to find out which components somebody else left on the tilemap, give the project the physics layers it shipped without, and separate what is solid from what is only painted. As one task it is unstartable, because most of the answer is already sitting in an Inspector and none of it is in a script.

First, before any code: cut that into parts small enough that you could look each one up on its own.

  1. Read the three components you inheritedwhat does this part do?Read: the component table in the first activity on this page, and the Inspector reading you did on this same tilemap in 6.2
  2. Prove the composite is really mergingwhat does this part do?Read: the Composite Operation row in the tilemap collider's Inspector, and the merge question in the first Socratic on this page
  3. Create the layers the project shipped withoutwhat does this part do?Read: the Layers list in Edit → Project Settings → Tags and Layers, and the layer you created there in 6.2
  4. Assign each layer on the prefab, not the instancewhat does this part do?Read: the prefab-versus-instance rule from 2.1
  5. Point the ground mask at a layer that existswhat does this part do?Read: 6.2 — Ground Checks: No Infinite Jumps, where the mask field and the ground check live
  6. Decide the collision matrix, pair by pairwhat does this part do?Read: the layer collision matrix in Edit → Project Settings → Physics 2D
  7. Separate the solid from the paintedwhat does this part do?Read: the trap callouts in the In the Editor half of this page, and the three tilemaps in the Platformer scene
  8. Sort what draws in front of whatwhat does this part do?Read: the Sorting Layers list in Edit → Project Settings → Tags and Layers

Seven of those eight parts are Inspector work and not one of them is code — which is why the split has to be written down. A task you cannot see as a list of places to go and look is a task you will try to solve by writing a script, and there is no script that can help you here. Look at the shape: the colliders were already there, the layers are names you type once, one of the two lists called "layer" has nothing to do with physics, and the last two parts are decisions about categories rather than about objects.

The test for a finished split: does the part name something you could open and point at — a component, a list, a checkbox, a prefab? "Make the tilemap solid" points at nothing, and it is also already done. "Which setting feeds the composite" points at one checkbox, and that is a part.

Your turn

Pick two of the parts above and say in one line what each one actually does.

You are adding four layers, and every one is an empty bucket until something is put in it. Name one object in this project that sits on Default today and should not, and say what goes wrong the day a mask is pointed at Default instead of at a category you chose.

Two sorting layers in this project are placeholders nobody replaced. Fix them before you build on top of them, and say what a placeholder name costs you three weeks from now.

In the Editor ~40 min

Press Play and stand on the floor, then Hierarchy → select Collision and read every component on it in the Inspector. Three of them you did not add, and each is load-bearing:

ComponentWhat it contributesWhat it costs
Tilemap Collider 2D Turns tiles into physics shapes: one small collider per solid tile, so the cave becomes a few hundred shapes. Every shape is a candidate in every physics query. It works, and it is quietly expensive.
Composite Collider 2D Fuses those shapes into one merged outline: the tilemap collider feeds it, and it publishes a single clean polygon. One more component to understand, and one setting that decides whether it is used at all.
Rigidbody 2D Unity adds this with the composite. It lets the merged shape be a body the physics engine owns. Nothing, if you leave it alone — but it must stay Static. A floor that moves is a bug in a cave.

So the work is not adding collision. Work the list in order — every step names its pane.

  1. Inspector → the Tilemap Collider 2D → the Composite Operation dropdown. It reads Merge, and that setting decides whether the composite above it is used at all (older docs call the checkbox Used By Composite). Set it to None, press Play and walk: you still stand up, and the seams come back as lips you catch on. Set it back to Merge.
    Inspector showing CollisionInspectorCollisionTransformTilemapTilemap RendererRigidbody 2DBody TypeStaticGravity Scale1Composite Collider 2DTilemap Collider 2DIs TriggerUsed By Effector1Composite OperationMergeOffsetX 0 Y 0Add Component
    The Collision tilemap with every component on it, in the order Unity shows them. Tilemap Collider 2D is last because that is where it sits — open it and Composite Operation is the row that reads Merge, the setting that decides whether the composite above it is used at all. Gravity Scale is greyed for a reason: a Static body is never moved by the engine, so there is nothing for that 1 to act on.
  2. Edit ▸ Project Settings ▸ Tags and Layers → compare the two lists. Tags holds six names, two of them Ground and GroundCheck. Layers is where the gap shows: as the project shipped it held the Unity defaults, a run of empty slots and three placeholders called Layer 1, Layer 2 and Layer 3 — and no Ground layer at all. 6.2 is where you typed one into the first empty slot; confirm it is still there, because every mask in this unit points at it.
  3. Same window → finish the list. Ground is one layer, and one bucket is not a layer system. Add Player, Enemy, Projectile and Decoration, renaming empty slots as you go.
  4. Project window → Assets/Prefabs → Inspector on each prefab → assign them at the source. The two solid tilemaps become Ground, the Player prefab Player, the enemy and turret prefabs Enemy, the projectile prefab Projectile, and anything you paint that is only scenery Decoration. A layer set on one instance is a fact about that object; on the prefab, every copy inherits it.
  5. Hierarchy → the Player → Inspector → the 6.2 ground-check component. Point its mask field at Ground and nothing else, then test the claim: stand next to an enemy and ask the ground check whether you are grounded.
  6. Edit ▸ Project Settings ▸ Physics 2D → the layer collision matrix, the grid of tick-boxes. Decide, pair by pair, who collides with whom, then justify two ticks: Enemy against Enemy? Projectile against Decoration?
Already done for you — do not do it twice. Follow an older tutorial that adds a second Tilemap Collider 2D to Collision and you do not get twice the colliders: you get duplicated shapes and edge behaviour you will blame on your movement code. Read the Inspector before you add any component in this unit.
Layer or tag — they are not the same tool, and this project shipped with only one of them. The Collision tilemap carries the tag Ground, and that tag is what your CompareTag() checks read. But a raycast mask cannot use a tag — it needs a layer, and the project you were handed had none. The part that catches people: a mask naming a layer that does not exist does not quietly widen to everything. LayerMask.GetMask("Ground") in a project with no Ground layer logs an error and hands back a mask with no bits set — and an empty mask is not a wide mask. The ray notices nothing, the ground check reads false on a floor you can see, and the player jumps in mid-air forever. An unset Inspector dropdown is the same silence by another route: Unity draws it as Nothing. Prove it once, on purpose, by renaming the layer and watching the check stop working. Tags identify one object; layers filter whole categories in a query.
Painting decoration onto the solid tilemap is the trap this whole lesson exists to close. A pebble, a vine, a distant rock — all of them look correct painted on Collision, and all of them become rock. Then the thing standing on the pebble is you. Two fixes are defensible; choose one and defend it: paint decoration onto the No Collision tilemap (simple, and it can never lie to you), or keep it on a solid tilemap and give that tilemap a Decoration layer your queries exclude (more flexible, more to remember).
In the Code ~15 min

No new file today, so this half holds the two specifications. Write your answers down.

Architectural specification: the ground check you already own. In 6.2 you wrote a LayerMask field and passed it into Physics2D.Raycast. Open that script and answer before you touch anything:
  • What is that mask field currently set to in the Inspector? Not what you intended — what is actually ticked in the dropdown.
  • Name the two solid surfaces in this cave that a Ground mask includes and a Default mask does not — and say which of the two you would have forgotten to put in the bucket.
  • The tag Ground already exists and is already on the Collision tilemap. If you could fix this with a tag instead of a layer, what would the raycast call look like, and what would it cost you every frame?
Architectural specification: sorting, the other kind of layer.
  • Read the Sorting Layers list. It contains Bottom, Player and Top — and also Layer 2 and Layer 4, placeholders nobody ever replaced. Read them as they are, then say which of the two lists answers "who draws in front".
  • Your backdrop, your ground, your player and your attack swing all need an order. Assign a sorting layer to each, and say why the swing effect needs its own layer above Player. Two sprites on the same layer still need an order: name that second knob and where it lives.
  • Is a sorting layer a physics object in any way — can changing one stop the player falling through the floor? Prove it from the two lists.

Socratic: Reading a Component You Did Not Write

  1. The tilemap collider builds a shape per tile, and the composite merges them. With the merge switched off, how many physics shapes would the cave have?
  2. The rigidbody is Static. What does Static mean about the body's willingness to move, and why is that the correct choice for a cave floor rather than an oversight?
  3. Standing on this floor, your ground check from 6.2 succeeds. So does the raycast care whether it hit one merged polygon or four hundred separate tile shapes?
Read the Docs: One-Way Platforms ~15 min

A ledge you can jump up through is not a layer's job — it is a collider property with a direction (the effector's Surface Arc decides which way is up). Three honest ways to build one:

ApproachHow it worksWhat it costsWhen it wins
A separate one-way tilemap — its own Tilemap Collider 2D + Composite Collider 2D + Platform Effector 2D, with Used By Effector ticked The tilemap becomes a membrane you pass upward and stand on downward. A third tilemap to maintain, and one direction for every tile on it. Many one-way ledges, all behaving the same.
Per-tile effector — Platform Effector 2D plus Used By Effector on the tilemap collider Individual tiles in the existing tilemap gain a direction. It fights the composite merge, and you get two kinds of tile on one grid. Rarely. Know it exists; do not reach for it first.
A loose object — a GameObject with a Box Collider 2D + Platform Effector 2D, placed over a gap in the tiles One platform, one object, zero tilemap surgery. It is no longer part of the painted level, so it can drift out of alignment with the grid. You need exactly one or two, and you need them now.

Choose, Then Justify

  1. Which of the three approaches did you build, and what in your level made it the right one?
  2. A one-way platform that is one tile thick: what happens to the player who is inside it when the effector is added mid-game? Predict before testing.
  3. If a one-way ledge sits on a layer excluded from the player's mask, does the player still stand on it? Which system decides that — the layer mask or the effector?

Checkpoint — before moving on, you must be able to:

Reference Docs

Need a deeper explanation? These pages cover the same concepts:

Next: 7.3 — Ladders I: The Climbing State.

0/9 answered

Answers save on this device as you type. At the end of class, click Download and upload the file to the Canvas assignment.

Saved on this device only — the Dashboard tracks your progress.