In 2D, Godot draws by z_index first, then by Y-sort, then by node order, and z_index adds up through parents unless it is set as absolute. So a hit spark added the plain way ends up behind the enemy more often than you would think. In our Godot 4.7 test a 16 px spark on a 40 px enemy showed 0 of its 256 pixels in each of 4 common setups: the Effects node placed before the Enemies node, a Y-sorted world with the spark above the enemy's feet, an enemy with z_index 2, and an enemy whose z_index of 1 sits under a parent of 1. The script below adds the effect with an absolute z_index one above the z the enemy is really drawn at: 256 of 256 in all 4.
The code
Save it as fx_on_top.gd and load it with const FxTop := preload("res://fx_on_top.gd"). Where you would add the hit effect, call FxTop.spawn(HIT.instantiate(), enemy, hit_position, $Effects): the effect, the node it hit, where, and the node to add it to.
extends RefCounted
## Add a 2D effect so it is drawn over the thing it hit, whatever the node order or Y-sort says.
## const FxTop := preload("res://fx_on_top.gd")
## FxTop.spawn(HIT.instantiate(), enemy, hit_position, $Effects)
static func spawn(effect: CanvasItem, over: CanvasItem, at: Vector2, parent: Node) -> CanvasItem:
parent.add_child(effect)
effect.global_position = at
effect.z_as_relative = false
effect.z_index = mini(drawn_z(over) + 1, RenderingServer.CANVAS_ITEM_Z_MAX)
return effect
static func drawn_z(item: CanvasItem) -> int:
## The z_index a node is really drawn at: relative z values add up through its parents.
var z := 0
var n: Node = item
while n is CanvasItem:
z += (n as CanvasItem).z_index
if not (n as CanvasItem).z_as_relative:
break
n = n.get_parent()
return z
What we measured
A blue 40 by 40 px enemy and a red 16 by 16 px effect placed 10 px above the enemy's centre, so the enemy could hide all of it. For each setup we added the effect the plain way (add_child, then global_position) and with the script, and counted the red pixels on the rendered screen. Two runs gave the same numbers.
- Effects node before the Enemies node in the scene tree: plain 0 of 256, script 256.
- Y-sorted world, Enemies Y-sorted too, effect added to the world: plain 0, script 256. The effect is higher on screen than the enemy's origin, so Y-sort draws it first.
- Enemy with z_index 2, effect added after it: plain 0, script 256.
- Enemies node with z_index 1, enemy with z_index 1 (relative, the default), so the enemy is drawn at 2: plain 0, script 256.
- drawn_z() on nodes of 3, 2 and 1 nested: 6. On a node of 1 under a node set to 5 that is not relative: 6.
Why it happens
Godot sorts 2D items by z_index first. Only items on the same z are then sorted by Y-sort, where a lower position on screen is drawn later, and last by their place in the scene tree. A z_index is relative to the parent by default, so an enemy of 1 under a parent of 1 is drawn at 2 while it says 1 in the Inspector. A hit effect sits on the upper half of the enemy, so Y-sort puts it behind, and an Effects node that comes first in the tree draws first. An absolute z_index above the enemy's real one wins over the other two.
What to change
- Effects that should stay under the characters, like footprints or a ground circle: keep them on a lower z instead, or under the Y-sorted world.
- Placing the spark where the hit landed: spawn a hit effect at the impact point. A spark that should flip with the attacker: flip a child node, not the body.
Free hit sparks to try it with: Free 2D Game VFX has 16 animated 2D effects as PNG sprite sheets for any engine, plus 10 noise and mask textures, free for commercial games.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Free 2D Game VFX has 16 animated 2D effects as PNG sprite sheets for any engine, plus 10 noise and mask textures, free for commercial games.
More how-tos: Play, stop and remove effects
- An aura that comes back when the buff comes back
- A spawned effect that appears exactly where you hit
- Remove a one-shot particle effect when it ends
- Fade out and stop looping particles
- Spawn a hit effect facing the surface
- Hit sparks from a pool of emitters
- Changing one particle effect's color changes all of them
- Particles that play the same way every time
- Freeze particles in mid-air
- Running dust and landing puffs
- Particles left behind by a moving object
- 2D particles follow the player or stay behind
- Footstep particles that match the ground
- A level up effect that plays while paused
- Hide particle effects far from the camera
- Death particles disappear when the enemy is freed
- Remove all particles at once
- Tire smoke that only shows when the car drifts
- Play a 2D sprite sheet effect once and remove it, even if its animation loops
- Sprite sheet frames without the empty cells
- Sprite sheet frames without lines from the next cell