← Yop2D

How to make the player lose health?

Beginner guide · no code

Français · English · Español · Deutsch · Italiano · Português · Русский · 中文 · 日本語

The player starts with 3 lives. Each time they touch a danger (spikes, an enemy…), they lose one and the health bar goes down. At 0, it's Game Over: the level starts again.

You need:

Step 1: prepare the dangers and the bar

  1. Select each danger and type danger in its “Tag (Label)” field.
  2. Select healthBar. In the inspector's “PROGRESS BAR” section: Minimum Value = 0, Maximum Value = 3, Current Value = 3.

Step 2: create the “vie” variable

  1. In the left panel, Variables section, create a variable.
  2. Name: vie (health) · Scope: Local · Type: Integer · Starting value: 3.

Step 3: lose a life

  1. Open the scene's Blueprint.
  2. Add the “If object touches Tag” event: target player, Tag danger.
  3. Add “Add to Variable”: variable vie, Value to add = -1 (with the minus sign, to subtract).
  4. Add “Set Progress”: target healthBar, New progress = =vie
  5. Link the three nodes in order.
If object touches Tagtarget: player · Tag: danger
→
Add to Variablevie · - 1
→
Set ProgresshealthBar · =vie
👉 Writing =vie in “New progress” means: “take the value of the vie variable”. So the bar always follows the number of lives.

Step 4: Game Over

  1. After “Set Progress”, add the “Condition” node with Condition = =vie <= 0 (“vie smaller than or equal to 0”).
  2. Link its True output to the “Restart the scene” node (category Scene & HUD).
  3. Save, start the game and touch a danger three times.
Set ProgresshealthBar
→
Condition=vie <= 0
True →
Restart the scene
👉 “Restart the scene” puts everything back as it was: objects in their place, and Local variables (so vie = 3) back to their starting value.
👉 Want a real end screen? Instead of restarting straight away, link the True output to “Modify Text” (a “You lose!” text), then “Wait” (2 seconds), then “Restart the scene”.

Bonus: falling into the void

For a platform game, put a Zone object at the very bottom of the scene, named fallZone (a zone is invisible during the game). Then:

Collision A/BObject A: player · Object B: fallZone
→
Restart the scene

If it doesn't work