Y-Sorting in 2D: Why Characters Draw Behind the Wrong Things
How y-sorting decides draw order in top-down 2D games, why characters end up behind tables and trees, and the settings in Godot 4, Unity and GameMaker.
In a top-down or three-quarter view game, things lower on the screen are closer to the viewer, so they should draw in front. Walk your character up behind a table and the table should cover its legs. Walk it in front and the character should cover the table. When that goes wrong, the character pops on top of furniture it is standing behind, or vanishes behind a tree it is clearly in front of.
Y-sorting fixes this by drawing objects in order of their y position: higher on screen first, lower on screen last. When characters still draw behind the wrong things, the cause is almost always one of three setup issues: y-sorting is not turned on for the right parent or axis, the point being sorted is not at the object's feet, or the objects are on different layers so their y position never gets compared.
How the sort decides
Each object gets one sort point, and the engine compares only that point's y value.
| Object | Sort point y | Draws |
|---|---|---|
| Character, feet at y = 400 | 400 | First (behind) |
| Table, base of its front legs at y = 420 | 420 | Second (in front of the character) |
The character's head may be far above the table top, but only the sort points are compared. So the sort point must be where the object touches the floor: the feet for a character, the base for a table, tree or crate.
In practice, the sort point is the object's position, which is its pivot or origin. If your pivots are at bottom center, y-sorting mostly works out of the box. If they are at the center, a tall character's sort point is at its waist and it sorts as if it were standing well behind where its feet are. Sprite pivot points covers setting pivots for every object type.
Why characters draw behind the wrong things
| Symptom | Cause | Fix |
|---|---|---|
| Characters always draw on top of everything (or always behind) | Y-sorting is not enabled, or objects are on different layers | Enable it on the shared parent or set the sort axis; put sortable objects on one layer |
| Character draws in front of a table it is behind | Sort point at sprite center, not the feet | Move the pivot to the feet, or sort by pivot instead of center |
| Tall tree pops in front of the character too early | Tree's sort point at its center | Sort point at the base of the trunk |
| A character's weapon or hat sorts separately from the body | Each part sorts on its own | Group the parts so they sort as one |
| Tiles like tree tops or wall faces sort wrong | Tile layer sorts by tile cell origin | Set a per-tile sort origin, or make those tiles objects |
| Long tables or diagonal walls look wrong at one end | One sort point cannot describe a long object | Split the object into pieces, or block the ambiguous area with colliders |
A single point cannot describe a 6-tile-long table: stand at one end and the comparison uses the table's middle. Split long objects into segments with their own sort points, or place collision so characters cannot stand where the sort is ambiguous. In top-down games the body collider should cover only the character's footprint, which helps here too; how big a 2D hitbox should be covers that sizing.
Godot 4
Godot y-sorts through the y_sort_enabled property that every CanvasItem has, shown in the Inspector as CanvasItem > Ordering > Y Sort Enabled.
How it behaves, per the CanvasItem class reference:
- When
y_sort_enabledis true on a node, that node and its children with a higher Y position are drawn in front of those with a lower Y position. When false, they draw in scene tree order. - If Y-sort is enabled on a parent A but disabled on a child B, then B is sorted, but B's own children render together at B's Y position. This is how you keep a character's body, weapon and shadow sorted as one unit.
- Nodes sort relative to each other only if they are on the same
z_index.
Setup:
- Make a parent
Node2D(say,World) for everything that sorts together, enable Y Sort on it, and put characters, props and the y-sorted tile layer under it. - Leave Y Sort disabled inside each character scene's root if you want the character's parts to sort as one unit at the root's position.
- Make sure each character's origin is at its feet. With
centeredon, setoffset.yto minus half the frame height so the sprite sits above the origin. - Keep sortable objects on the same
z_index. Use a differentz_indexonly for things that should always be above or below, like the floor. - For tiles, enable Y Sort on the
TileMapLayerand set Y Sort Origin on tiles in the TileSet. Godot describes it as the vertical point of the tile used for determining y-sorted order, in pixels. A tree tile whose trunk base sits 40 px below the tile's origin needs a Y Sort Origin of 40. TheTileMapLayeralso has its owny_sort_origin, added to every tile's value.
Unity
Unity sorts 2D renderers in a priority order: Sorting Layer and Order in Layer first, then render queue, then distance to camera, then Sorting Group, then material and shader, then a tiebreaker. Y-sorting happens in the "distance to camera" step, so it only applies between objects that share a Sorting Layer and Order in Layer.
- Set the sort axis. Set Transparency Sort Mode to Custom Axis and Transparency Sort Axis to X = 0, Y = 1, Z = 0. With Y at 1, an object higher up the screen renders behind one lower down. In URP, these settings are on the Renderer 2D asset. In the built-in render pipeline, they are in Edit > Project Settings > Graphics.
- Sort by pivot. On each Sprite Renderer, set Sprite Sort Point to Pivot. The default is Center, which measures from the center of the sprite. Pivot measures from the sprite's pivot position in world space.
- Put the pivot at the feet in the Sprite Editor for characters, and at the base for props.
- Use the same Sorting Layer and Order in Layer for everything that should y-sort against each other. Put the floor on a lower Sorting Layer.
- Add a Sorting Group to the root of any multi-sprite character. All renderers in the same Sorting Group share the same Sorting Layer, Order in Layer and distance to camera, so the body, weapon and hair sort as one object.
GameMaker
GameMaker draws by depth: the lower the depth value, the closer to the camera an instance is drawn. The manual's own example for top-down sorting is setting depth from y:
// Step event
if (y != yprevious)
{
depth = -y;
}
An instance lower on screen has a larger y, so a more negative depth, so it draws on top.
Things to know before you rely on it:
- Depth has limits. Instances outside the range of -16000 to 16000 are not drawn, although they still run their events. With
depth = -y, anything below y = 16000 disappears. For very tall rooms, offset the formula, for exampledepth = -y + 8000, as long as the result stays in range. - Equal depths draw in either order. Two instances at the same y may swap. If one must always win, offset its depth by 1.
- You cannot set depth in the Draw event. Set it in Step or End Step.
- Setting depth moves the instance to a managed layer. The built-in
layervariable then returns -1. The manual recommends discrete layers over setting depth in most cases, so keep floors, backgrounds and UI on normal layers and usedepth = -yonly for the sorted objects in between. - Origin at the feet.
yis the origin's position, so set sprite origins to Bottom Centre.
A test room for sorting
Keep one small room with a table, a tall tree and a long counter. Walk two characters of different heights in a slow circle around each object. One clean swap as the feet cross the object's base line is correct; a swap anywhere else means a sort point is wrong. Characters of very different heights are the best stress test, which character scale relationships explores from the design side.
Doing this in Makko
In Code Studio, the Asset Manager anchors characters at bottom center when it right-sizes them against a reference animation, so their positions are already at the feet, which is the sort point y-sorting needs. For props and environment pieces, set the pivot at the base before placing them. For keeping those props and rooms visually consistent with your characters, see how to build a consistent game world with AI.
FAQ
What is y-sorting in 2D games?
Y-sorting draws objects in order of their vertical position, so objects lower on the screen draw in front of objects higher up. It makes top-down and three-quarter view scenes read correctly when characters walk in front of and behind furniture, trees and each other.
Why is my character drawing behind objects in Godot?
Check that Y Sort Enabled is on for the shared parent, that the character and the object are children of that parent and on the same z_index, and that the character's origin is at its feet. If only some tiles sort wrong, set their Y Sort Origin in the TileSet.
How do I set up y-sorting in Unity?
Set Transparency Sort Mode to Custom Axis with Transparency Sort Axis (0, 1, 0), on the Renderer 2D asset in URP or in Project Settings > Graphics otherwise. Then set each Sprite Renderer's Sprite Sort Point to Pivot and keep sorted objects on the same Sorting Layer and Order in Layer.
How do I y-sort in GameMaker?
Set depth = -y in the Step event for the objects that should sort, with sprite origins at the feet. Keep the result between -16000 and 16000, because instances outside that depth range are not drawn.