← Pixel Dreamer Studio

Draw the hit effect over what it hit

Give the effect an absolute z_index one above the z the enemy is really drawn at.

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.

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

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