← Pixel Dreamer Studio

Let the shield take what it can and pass the rest of the hit to health

Absorb the smaller of the shield and the damage, and subtract the rest from health.

The usual first version is: if the shield is up, the hit goes to the shield, otherwise to health. In our Godot 4.7 test a 50 point shield and 100 health hit for 20, 20, 20, 30 and 15, 105 in all, ended with 55 health instead of 45, and the shield showed -10. One 80 point hit on a full shield did no health damage at all. Taking only what the shield can hold and passing the rest on gave 45, and 70 after the 80 point hit.

The code

Add the script to the character, call take_damage(amount) for every hit, and connect shield_broken to your shield break effect.

extends Node
## Energy shield that takes damage first. Whatever the shield cannot hold goes
## on to health, and the shield never shows less than 0.

signal shield_broken

var shield := 50.0
var health := 100.0


func take_damage(amount: float) -> void:
	var absorbed := minf(shield, amount)
	shield -= absorbed
	health = maxf(health - (amount - absorbed), 0.0)  # the rest of the hit
	if absorbed > 0.0 and shield <= 0.0:
		shield_broken.emit()

What we measured

Shield 50, health 100. Two runs with the same numbers.

Why it happens

Both first versions decide where the whole hit goes, so the part the shield could not hold disappears: 10 points of the third hit, 30 of the 80. Clamping hides the negative number, but it does not bring the lost damage back.

What to change

Shield that refills after a pause: a meter that refills while still being hit. Beams that stop at the shield: laser beam stops at the hit. A spinning laser that skips targets: check the swept arc.

Ready-made shield and beam effects: Sci-Fi & Energy VFX for Godot 4 has 33 ready-made sci-fi effects in Stylized and Retro PSX looks, including Laser, Plasma, Mining and Repair beams that stop where they hit, an Energy Shield Bubble and a Black Hole.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Sci-Fi & Energy VFX for Godot 4 has 33 ready-made sci-fi effects in Stylized and Retro PSX looks, including Laser, Plasma, Mining and Repair beams that stop where they hit, an Energy Shield Bubble and a Black Hole.

More how-tos for Sci-Fi & Energy VFX for Godot 4: A laser beam that stops where it hits, the moment you aim

More how-tos: Hazards, traps and attacks