9 health bar tricks for a 2D game, from HP text to a bar that lies
Nine health bar tricks stacked on one fight, from plain HP text to a smooth drain, a damage trail, shake and flash, a heal trail, color by health, a low HP warning and a last sliver that lasts longer than it looks. With the timing numbers and tested Godot code.
Paste it into Claude Code, Codex, Grok Build or any coding agent.
Copied. Want one of these every week?
The week's game techniques in one free email on Sunday, each with a prompt like this one.
# Add the full set of health bar tricks to my game
My game's health bar just shows the current HP, so getting hit or healed doesn't feel like much. Upgrade it with seven tricks that stack: a smooth drain, a damage trail, shake and flash on hit, a heal trail, color by health, a low HP warning, and a last sliver that holds more health than it shows.
Before you write code, look at my project: the engine and language, where the player's HP lives, where damage and healing happen, and how the HUD draws the current bar. Upgrade the bar I have instead of adding a second one, and keep its size, position and art. If my bar shows HP numbers, ask me before turning on the sliver trick, because it doesn't work with numbers. If I have no health bar yet, ask me where it should go.
## How it works
- Three lengths, each 0 to 1 of the bar: the fill, a pale trail behind it after damage, a white heal chunk ahead of it after a heal. Draw back to front: empty, trail, heal chunk, fill.
- Damage and heal only change the HP and remember where the fill was. The rest is worked out every frame from the time since the last hit and heal.
- With the trail on, the fill snaps and the trail is the slow part (it replaces the drain).
- One function turns HP into a bar length (the sliver trick). Everything that draws uses it.
- The low HP pulse is a value from 0 to 1 the rest of the game can read (screen edges, a heartbeat sound).
## Numbers that matter
All times are in seconds since the hit or heal, counted from the first frame after it.
- Drain: 0.5 s, eased out with `1 - (1 - u)^1.4`. Halfway it's at 81% on the way from 100% to 70%.
- Damage trail: the fill snaps, the trail holds 0.2 s, then catches up over 0.25 s with a smoothstep (done 0.45 s after the hit).
- Shake: 0.3 s with an amplitude of 28 px sideways on a 1160 px wide bar (scale with my bar's width) and a third of that up and down, decaying with `(1 - t / 0.3)^1.5`, 12 sways a second sideways and 9 up and down. At 60 fps it really moves at most about 19 px. Flash: the fill is white while less than 0.07 s has passed (the first 4 frames at 60 fps).
- Heal trail: the white chunk jumps to the new HP at once, the fill waits 0.15 s, then grows into it over 0.5 s with a smoothstep.
- Color: green above 50%, yellow above 25%, red below. Below 50% blend green to yellow with `smoothstep((50 - p) / 6)`, below 25% yellow to red with `smoothstep((25 - p) / 6)`, p = fill in percent. Never blend green straight to red.
- Low HP warning: when the fill is under 25% and HP is above 0, pulse = `smoothstep(low_time / 0.15) * (0.5 - 0.5 * cos(low_time * 2 * pi * 2.5))`. low_time is 0 on the first low frame, so the first peak is 0.2 s in, then one every 0.4 s. It stops at 25% or more.
- Sliver: the last 40 HP of 100 are drawn as the last 10% of the bar. 100, 70, 40 and 10 HP fill 100%, 55%, 10% and 2.5%.
- Colors (RGB) if my bar has none: frame (22, 14, 26), 8 px on a 134 px tall bar, empty (58, 28, 40), fill (228, 52, 60), green (84, 196, 84), yellow (242, 184, 52), trail (255, 236, 186), heal chunk (250, 250, 246). The pulse moves the frame toward (255, 64, 72) by `pulse` and the fill toward (255, 150, 150) by `0.35 * pulse`.
- Make every number a setting in one place. Each trick needs an off value (0 or false) that brings back the plain bar.
## Pseudo code
```
bar_fraction(hp):
if sliver_width <= 0: return hp / max_hp
if hp < sliver_hp: return hp / sliver_hp * sliver_width
return sliver_width + (hp - sliver_hp) / (max_hp - sliver_hp) * (1 - sliver_width)
damage(amount):
hp = max(hp - amount, 0)
trail_from = trail if trail_time > 0 else fill
from = fill
heal_to = min(heal_to, bar_fraction(hp))
since_hit = 0
heal(amount):
hp = min(hp + amount, max_hp)
from = fill
heal_to = bar_fraction(hp)
since_heal = 0
every frame (dt):
since_hit += dt; since_heal += dt
target = bar_fraction(hp)
if since_heal < since_hit and heal_time > 0:
fill = lerp(from, target, smoothstep((since_heal - heal_delay) / heal_time))
else if target < fill and drain_time > 0 and trail_time <= 0:
u = min(since_hit / drain_time, 1)
fill = lerp(from, target, 1 - (1 - u)^1.4)
else:
fill = target
if trail_time > 0:
trail = max(lerp(trail_from, fill, smoothstep((since_hit - trail_hold) / trail_time)), fill)
else: trail = fill
heal_to = max(heal_to, fill)
shake = (0, 0)
if since_hit < shake_time:
d = (1 - since_hit / shake_time)^1.5
shake = (shake_px * d * cos(since_hit * 2pi * 12), shake_px / 3 * d * sin(since_hit * 2pi * 9))
if fill < low_hp_at and hp > 0:
low_time = 0 if low_time < 0 else low_time + dt
pulse = smoothstep(low_time / 0.15) * (0.5 - 0.5 * cos(low_time * 2pi * 2.5))
else: low_time = -1; pulse = 0
draw at offset shake: frame lerped toward red by pulse, empty, trail (pale), heal chunk (white),
fill (white if since_hit < flash_time, else color by fill, lightened by pulse)
```
`smoothstep(u)` clamps u to 0..1 and returns `u * u * (3 - 2u)`. Start with since_hit and since_heal at a large number. In Godot, run this in `_process` and draw with `draw_rect` in `_draw`; in Unity, run it in `Update` with `Time.deltaTime` and set three UI Images' `fillAmount`; in a custom engine, draw three rectangles.
## Pitfalls
- A slow drain under a trail hides the trail. Snap the fill when the trail is on.
- A hit during a heal must cut the white chunk back to the new HP. A hit during the 0.15 s wait snaps the fill up to the new HP; fine, or keep it growing.
- The warning must stop when the player dies, or it pulses over the game over screen.
- The sliver trick with numbers on the bar ("10 / 100" over 40 real HP) reads as a bug.
## Check that it works
Make a small automated test that steps the bar at a fixed 60 fps (call the per-frame update with dt = 1/60, no real time), or a debug key that deals 30 damage and one that heals 60.
- Every trick off: 30 damage snaps the fill to 70% on the first frame, no trail, flash or shake.
- Drain only: the fill reaches 70% on frame 30 after the hit (0.5 s) and is near 81% on frame 15.
- Trail on: the fill is 70% on the first frame, the trail stays at 100% for 0.2 s and is at 70% from 0.45 s.
- The flash covers the first 4 frames after a hit, the shake the first 0.3 s.
- From 10 HP, a heal of 60 shows the white chunk at 70% at once; the fill starts moving after 0.15 s and is at 70% 0.65 s after the heal.
- Colors: green at 70%, yellow at 40%, red at 10%.
- The warning is off at 70% and 40%, and at 10% it peaks every 24 frames.
- Sliver on: after two hits of 30 the bar shows 10% and the warning is on, while a plain bar shows 40% with no warning.
- Turn every setting off and the bar behaves exactly like my old one.
Source: SpriteCook (2D game assets made by your coding agent, for Claude, ChatGPT and Cursor)
Full guide with a video of all nine levels, pictures of each trick and the tested Godot script: https://www.spritecook.ai/blog/health-bar-ui-damage-trail-low-hp-warningMy first health bar was the text "HP: 70" in a corner. Every game since, I added another small trick, and each one made getting hit feel a bit better. Here are all nine, stacked on the same fight, so you can see what each one adds.
Same fight in every level: the slime hits for 30, three times. Each level keeps the tricks before it and adds one. The hero, the slime, the potion and the cave were all generated by a coding agent with SpriteCook.
The setup
One bar, 100 HP, hits of 30. Every trick below is one setting in one Godot script, and setting it to 0 turns it off. Times are in seconds and were tested frame by frame at 60 fps.
1. HP as text
HP: 70 in a corner. It works, and it's where everyone starts. The problem is that nobody reads numbers in the middle of a fight, so you only find out how hurt you are once you look away from the action.
2. A health bar
The same number as a bar. You can read it from the corner of your eye, which is the whole point. This one snaps: the hit lands and the bar is shorter on the same frame.
3. Smooth drain
Instead of snapping, the bar slides down over 0.5 s. It eases out, so most of the drop happens early: halfway in time it has already done about two thirds of it (81% on the way from 100% to 70%). Your eye catches the movement, so you notice the hit even when you weren't looking at the bar.
4. Damage trail
The fill snaps to the new value right away, and a pale chunk stays where the health used to be. It waits 0.2 s, then catches up over 0.25 s. Fighting games and Souls games do this. You get the exact new value at once, and the chunk shows how much that one hit took.
The trail replaces the drain. With both on, the drain hides most of the chunk, so in the code the trail takes over when it's on.
5. Shake and flash
On a hit, the bar shakes for 0.3 s and the fill turns white for 0.07 s (4 frames). The shake swings 28 px sideways and a third of that up and down, and dies out so fast that at 60 fps the bar never moves more than about 19 px. That's on a bar 1160 px wide, so scale it to yours. The bar now reacts the way the hero does, so the hit lands twice.
6. Heal trail
The damage trail in reverse. When the hero drinks a potion, a white chunk shows the new health right away. The fill waits 0.15 s, then grows into it over 0.5 s. You see what you're getting back before you have it, and the growing fill feels like the potion working.
7. Color by health
Green above 50%, yellow above 25%, red below. Each change starts at its threshold and is done 6% lower (green to yellow from 50% to 44%), so it never pops. Go green to yellow to red: a straight blend from green to red turns muddy brown in the middle.
8. Low HP warning
Below 25%, the bar's frame and fill pulse red 2.5 times a second (a peak every 24 frames). The pulse fades in over 0.15 s, so it doesn't start with a jump. The script exposes the pulse as a number from 0 to 1, so the rest of the game can follow it: in the clip the screen edges glow red with it. A heartbeat sound on the same beat works too.
9. The last sliver lies
This is the one most players never notice. In my version the last 40 HP are drawn as the last 10% of the bar, and everything above that is spread over the other 90%. The slime still hits for 30, exactly like in level 1. But after two hits, with 40 HP left, the bar is already a thin sliver and the low HP warning is going. You play like you're about to die, then the third hit lands and you're still standing on a sliver with 10 HP.
The white strip under the bar is only there to show you the real HP. It's not part of the game. The idea comes from a 2017 thread by designer Jennifer Scheurle, where she described games like Assassin's Creed and Doom making "your last shred of life slightly more durable than it should be" (Kotaku). The 40 HP and 10% are my numbers, not theirs.
It only works when the bar has no numbers on it. A bar that says "10 / 100" while it really means 40 is just a bug, so this game shows numbers only in level 1.
The code
All of it is one Godot 4 script, health_bar.gd, about 150 lines: download health_bar.gd. Add it to a Control, give the control a size, and call damage() and heal(). It draws itself, and every trick has its own setting at the top.
How much of the bar a given HP fills. This is the sliver trick, and with sliver_width at 0 it's a plain bar:
func bar_fraction(value: float) -> float:
var h := clampf(value, 0.0, max_hp)
if sliver_width <= 0.0 or sliver_hp <= 0.0:
return h / max_hp
if h < sliver_hp:
return h / sliver_hp * sliver_width
return sliver_width + (h - sliver_hp) / (max_hp - sliver_hp) * (1.0 - sliver_width)
Damage and heal only change the HP and remember where the bar was:
func damage(amount: float) -> void:
hp = maxf(hp - amount, 0.0)
trail_from = trail if trail_time > 0.0 else fill
from = fill
heal_to = minf(heal_to, bar_fraction(hp))
since_hit = 0.0
func heal(amount: float) -> void:
hp = minf(hp + amount, max_hp)
from = fill
heal_to = bar_fraction(hp)
since_heal = 0.0
Every frame, the bar works out the fill, the trail, the heal chunk, the shake and the pulse from the time since the last hit and heal:
func advance(delta: float) -> void:
since_hit += delta
since_heal += delta
var target := bar_fraction(hp)
if since_heal < since_hit and heal_time > 0.0: # healing: the fill grows into the chunk
var h := smoothstep(0.0, 1.0, (since_heal - heal_delay) / heal_time)
fill = lerpf(from, target, h)
elif target < fill and drain_time > 0.0 and trail_time <= 0.0: # smooth drain
var u := minf(since_hit / drain_time, 1.0)
fill = lerpf(from, target, 1.0 - pow(1.0 - u, 1.4))
else:
fill = target # snap (the trail shows the drop)
if trail_time > 0.0: # damage trail: wait, then catch up
var c := smoothstep(0.0, 1.0, (since_hit - trail_hold) / trail_time)
trail = maxf(lerpf(trail_from, fill, c), fill)
else:
trail = fill
heal_to = maxf(heal_to, fill)
shake = Vector2.ZERO # shake on hit
if shake_time > 0.0 and since_hit < shake_time:
var d := pow(1.0 - since_hit / shake_time, 1.5)
var t := since_hit * TAU
shake = Vector2(shake_px * d * cos(t * 12.0), shake_px / 3.0 * d * sin(t * 9.0))
if low_hp_at > 0.0 and fill < low_hp_at and hp > 0.0: # low HP warning
low_time = 0.0 if low_time < 0.0 else low_time + delta
pulse = smoothstep(0.0, 1.0, low_time / 0.15) * (0.5 - 0.5 * cos(low_time * TAU * pulse_hz))
else:
low_time = -1.0
pulse = 0.0
Tested in Godot 4.6, frame by frame at 60 fps:
- With every setting off, 30 damage snaps the bar to 70% on the hit frame, with no trail, flash or shake.
- The drain lands on 70% 0.5 s after the hit and is at 81% halfway.
- With the trail on, the fill snaps to 70%, the pale chunk holds at 100% for 0.2 s and has caught up 0.45 s after the hit.
- The shake moves the bar for 0.3 s, at most 19 px sideways, and the fill is white for the first 4 frames.
- A heal of 60 from 10 HP shows the white chunk up to 70% at once; the fill starts growing after 0.15 s and reaches 70% 0.65 s after the heal.
- The fill is green at 70%, yellow at 40% and red at 10%.
- The warning stays off at 70% and 40%; at 10% it pulses with a peak every 24 frames.
- With the sliver on, 100, 70, 40 and 10 HP fill 100%, 55%, 10% and 2.5% of the bar, and more HP always fills more bar. After two hits it pulses at 40 HP, where a plain bar waits for the third hit.
- The script matches the Python version that drew the pictures on this page on every frame.
Other engines
The logic has no Godot in it: keep the time since the last hit and heal, and work out the fill, trail and heal chunk from those every frame. In Unity, run it in Update with Time.deltaTime and draw the three layers as UI Images with fillAmount, the trail and heal chunk behind the fill. In Phaser or your own engine, draw three rectangles. I only tested the Godot version.
Common pitfalls
Numbers on a lying bar. If the bar says "10 / 100", players do the math, and a sliver that survives a hit of 30 looks broken. Use the sliver trick only on a bar without numbers.
A drain under a trail. A slow drain covers the pale chunk, so the trail never shows. Snap the fill when the trail is on and let the chunk do the slow part.
A warning that never stops. The pulse has to end when the bar goes back above 25% and when the player dies, or it plays over the game over screen.
A heal and a hit at once. A hit during a heal has to cut the white chunk back to the new HP, or the bar promises health you no longer have.
Green to red in one blend. Halfway between green and red is brown. Go through yellow, and blend each step over a few percent so the color doesn't pop.
Where to next
- Hit stop, hit flash, knockback and screen shake, what the hero and the camera do on the same hit
- 13 ways to show invincibility frames in a 2D game, for the moment right after the hit
- How to make the liquid in pixel art potions slosh, a potion worth drinking
- Game developers explain some of their favorite ways to trick players on Kotaku, the 2017 thread behind the last trick
Add this to your game with your coding agent
The prompt has the numbers, pseudo code, pitfalls and checks from this guide, written so your agent can fit it into your own project.
Copied. Want one of these every week?
The week's game techniques in one free email on Sunday, each with a prompt like this one.
# Add the full set of health bar tricks to my game
My game's health bar just shows the current HP, so getting hit or healed doesn't feel like much. Upgrade it with seven tricks that stack: a smooth drain, a damage trail, shake and flash on hit, a heal trail, color by health, a low HP warning, and a last sliver that holds more health than it shows.
Before you write code, look at my project: the engine and language, where the player's HP lives, where damage and healing happen, and how the HUD draws the current bar. Upgrade the bar I have instead of adding a second one, and keep its size, position and art. If my bar shows HP numbers, ask me before turning on the sliver trick, because it doesn't work with numbers. If I have no health bar yet, ask me where it should go.
## How it works
- Three lengths, each 0 to 1 of the bar: the fill, a pale trail behind it after damage, a white heal chunk ahead of it after a heal. Draw back to front: empty, trail, heal chunk, fill.
- Damage and heal only change the HP and remember where the fill was. The rest is worked out every frame from the time since the last hit and heal.
- With the trail on, the fill snaps and the trail is the slow part (it replaces the drain).
- One function turns HP into a bar length (the sliver trick). Everything that draws uses it.
- The low HP pulse is a value from 0 to 1 the rest of the game can read (screen edges, a heartbeat sound).
## Numbers that matter
All times are in seconds since the hit or heal, counted from the first frame after it.
- Drain: 0.5 s, eased out with `1 - (1 - u)^1.4`. Halfway it's at 81% on the way from 100% to 70%.
- Damage trail: the fill snaps, the trail holds 0.2 s, then catches up over 0.25 s with a smoothstep (done 0.45 s after the hit).
- Shake: 0.3 s with an amplitude of 28 px sideways on a 1160 px wide bar (scale with my bar's width) and a third of that up and down, decaying with `(1 - t / 0.3)^1.5`, 12 sways a second sideways and 9 up and down. At 60 fps it really moves at most about 19 px. Flash: the fill is white while less than 0.07 s has passed (the first 4 frames at 60 fps).
- Heal trail: the white chunk jumps to the new HP at once, the fill waits 0.15 s, then grows into it over 0.5 s with a smoothstep.
- Color: green above 50%, yellow above 25%, red below. Below 50% blend green to yellow with `smoothstep((50 - p) / 6)`, below 25% yellow to red with `smoothstep((25 - p) / 6)`, p = fill in percent. Never blend green straight to red.
- Low HP warning: when the fill is under 25% and HP is above 0, pulse = `smoothstep(low_time / 0.15) * (0.5 - 0.5 * cos(low_time * 2 * pi * 2.5))`. low_time is 0 on the first low frame, so the first peak is 0.2 s in, then one every 0.4 s. It stops at 25% or more.
- Sliver: the last 40 HP of 100 are drawn as the last 10% of the bar. 100, 70, 40 and 10 HP fill 100%, 55%, 10% and 2.5%.
- Colors (RGB) if my bar has none: frame (22, 14, 26), 8 px on a 134 px tall bar, empty (58, 28, 40), fill (228, 52, 60), green (84, 196, 84), yellow (242, 184, 52), trail (255, 236, 186), heal chunk (250, 250, 246). The pulse moves the frame toward (255, 64, 72) by `pulse` and the fill toward (255, 150, 150) by `0.35 * pulse`.
- Make every number a setting in one place. Each trick needs an off value (0 or false) that brings back the plain bar.
## Pseudo code
```
bar_fraction(hp):
if sliver_width <= 0: return hp / max_hp
if hp < sliver_hp: return hp / sliver_hp * sliver_width
return sliver_width + (hp - sliver_hp) / (max_hp - sliver_hp) * (1 - sliver_width)
damage(amount):
hp = max(hp - amount, 0)
trail_from = trail if trail_time > 0 else fill
from = fill
heal_to = min(heal_to, bar_fraction(hp))
since_hit = 0
heal(amount):
hp = min(hp + amount, max_hp)
from = fill
heal_to = bar_fraction(hp)
since_heal = 0
every frame (dt):
since_hit += dt; since_heal += dt
target = bar_fraction(hp)
if since_heal < since_hit and heal_time > 0:
fill = lerp(from, target, smoothstep((since_heal - heal_delay) / heal_time))
else if target < fill and drain_time > 0 and trail_time <= 0:
u = min(since_hit / drain_time, 1)
fill = lerp(from, target, 1 - (1 - u)^1.4)
else:
fill = target
if trail_time > 0:
trail = max(lerp(trail_from, fill, smoothstep((since_hit - trail_hold) / trail_time)), fill)
else: trail = fill
heal_to = max(heal_to, fill)
shake = (0, 0)
if since_hit < shake_time:
d = (1 - since_hit / shake_time)^1.5
shake = (shake_px * d * cos(since_hit * 2pi * 12), shake_px / 3 * d * sin(since_hit * 2pi * 9))
if fill < low_hp_at and hp > 0:
low_time = 0 if low_time < 0 else low_time + dt
pulse = smoothstep(low_time / 0.15) * (0.5 - 0.5 * cos(low_time * 2pi * 2.5))
else: low_time = -1; pulse = 0
draw at offset shake: frame lerped toward red by pulse, empty, trail (pale), heal chunk (white),
fill (white if since_hit < flash_time, else color by fill, lightened by pulse)
```
`smoothstep(u)` clamps u to 0..1 and returns `u * u * (3 - 2u)`. Start with since_hit and since_heal at a large number. In Godot, run this in `_process` and draw with `draw_rect` in `_draw`; in Unity, run it in `Update` with `Time.deltaTime` and set three UI Images' `fillAmount`; in a custom engine, draw three rectangles.
## Pitfalls
- A slow drain under a trail hides the trail. Snap the fill when the trail is on.
- A hit during a heal must cut the white chunk back to the new HP. A hit during the 0.15 s wait snaps the fill up to the new HP; fine, or keep it growing.
- The warning must stop when the player dies, or it pulses over the game over screen.
- The sliver trick with numbers on the bar ("10 / 100" over 40 real HP) reads as a bug.
## Check that it works
Make a small automated test that steps the bar at a fixed 60 fps (call the per-frame update with dt = 1/60, no real time), or a debug key that deals 30 damage and one that heals 60.
- Every trick off: 30 damage snaps the fill to 70% on the first frame, no trail, flash or shake.
- Drain only: the fill reaches 70% on frame 30 after the hit (0.5 s) and is near 81% on frame 15.
- Trail on: the fill is 70% on the first frame, the trail stays at 100% for 0.2 s and is at 70% from 0.45 s.
- The flash covers the first 4 frames after a hit, the shake the first 0.3 s.
- From 10 HP, a heal of 60 shows the white chunk at 70% at once; the fill starts moving after 0.15 s and is at 70% 0.65 s after the heal.
- Colors: green at 70%, yellow at 40%, red at 10%.
- The warning is off at 70% and 40%, and at 10% it peaks every 24 frames.
- Sliver on: after two hits of 30 the bar shows 10% and the warning is on, while a plain bar shows 40% with no warning.
- Turn every setting off and the bar behaves exactly like my old one.
Source: SpriteCook (2D game assets made by your coding agent, for Claude, ChatGPT and Cursor)
Full guide with a video of all nine levels, pictures of each trick and the tested Godot script: https://www.spritecook.ai/blog/health-bar-ui-damage-trail-low-hp-warningMore game techniques
All techniques
9 ways to make a platformer jump feel good, from variable height to wall jumpsA plain gravity jump works, but it feels stiff and unfair. Nine small tricks that fix it, from variable jump height, fast fall and apex hang to coyote time, jump buffering, corner and ledge correction, wall jump reach and squash and stretch, with Celeste's numbers, tested Godot code and a prompt for each.