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.
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.
- 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
- 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
- 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
- Assign each layer on the prefab, not the instancewhat does this part do?Read: the prefab-versus-instance rule from 2.1
- 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
- Decide the collision matrix, pair by pairwhat does this part do?Read: the layer collision matrix in Edit → Project Settings → Physics 2D
- 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
- 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.
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:
| Component | What it contributes | What 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.
-
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.
The Collisiontilemap with every component on it, in the order Unity shows them.Tilemap Collider 2Dis last because that is where it sits — open it andComposite Operationis the row that readsMerge, the setting that decides whether the composite above it is used at all.Gravity Scaleis greyed for a reason: aStaticbody is never moved by the engine, so there is nothing for that 1 to act on. -
Edit ▸ Project Settings ▸ Tags and Layers → compare the two lists.
Tags holds six names, two of them
GroundandGroundCheck. Layers is where the gap shows: as the project shipped it held the Unity defaults, a run of empty slots and three placeholders calledLayer 1,Layer 2andLayer 3— and noGroundlayer 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. -
Same window → finish the list.
Groundis one layer, and one bucket is not a layer system. AddPlayer,Enemy,ProjectileandDecoration, renaming empty slots as you go. -
Project window →
Assets/Prefabs→ Inspector on each prefab → assign them at the source. The two solid tilemaps becomeGround, the Player prefabPlayer, the enemy and turret prefabsEnemy, the projectile prefabProjectile, and anything you paint that is only sceneryDecoration. A layer set on one instance is a fact about that object; on the prefab, every copy inherits it. -
Hierarchy → the Player → Inspector → the 6.2 ground-check component.
Point its mask field at
Groundand nothing else, then test the claim: stand next to an enemy and ask the ground check whether you are grounded. -
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:
EnemyagainstEnemy?ProjectileagainstDecoration?
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.
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.
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).
No new file today, so this half holds the two specifications. Write your answers down.
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
Groundmask includes and aDefaultmask does not — and say which of the two you would have forgotten to put in the bucket. - The tag
Groundalready exists and is already on theCollisiontilemap. 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?
- Read the Sorting Layers list. It contains
Bottom,PlayerandTop— and alsoLayer 2andLayer 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
- 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?
- 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?
- 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?
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:
| Approach | How it works | What it costs | When 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
- Which of the three approaches did you build, and what in your level made it the right one?
- 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.
- 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:
- Name the three components already on
Collision, and what each does - Explain what a composite fixes, and the setting that switches it on
- Say out loud why
Groundbeing a tag was not enough, and extend the physics layers - Explain the difference between a physics layer and a sorting layer, with one use of each
- Fix your 6.2 ground-check mask and prove the fix with a test
- Defend your choice of one-way platform approach
- Break this feature into parts small enough to look each one up on its own, and say in one line what each part does
- Say which earlier lesson each part of this feature came from, and where you would reread it
- Commit and push
Reference Docs
Need a deeper explanation? These pages cover the same concepts:
- Unit 7: Level Building & Ladders
- Unity + C# Quick Reference — collider, layer and
CompareTag()rows
Saved on this device only — the Dashboard tracks your progress.