Help pls (heart health system)

So it’s my first game and I am stuck on how to create and incorporate a heart health system into my game. I currently have a number health system (so it shows a 3 then when the player takes damage it minuses 1 off of that and when it reaches zero, then the game ends) and I’ve tried to figure it out myself, and I’m struggling to do it. Can anyone help me?
I’ve added a script called “health” and it makes the hearts change into a different sprite after they are -(ed) from the damage taken but I don’t know how to apply the number system to the image (heart) system.

pls help (I’ve attached all of the scripts that have anything to do with the health)

5416851–550533–Health.cs (899 Bytes)
5416851–550536–Obstacle.cs (620 Bytes)
5416851–550542–Player.cs (1.88 KB)

@kimjonathan41

Well I bet you can find tutorials how to do that, but it is nothing but a percent calculation, what even I can do with my crappy math;

float healthPercent = health / maxHealth;

Then you can do the same for your health symbols, and compare if it is smaller than or equal to healthPercent, if so, show a heart.

You could use a switch statement if it’s only 3.

Case 1: 1 heart
Case 2: 2 hearts
Case 3: 3 hearts

1 Like

@knobblez

yeah, I started to think that too…

If he simply needs health in stars, make the health match stars… 5 stars for 5 health… so character health is just 5, not 100 or something like that. Somehow I started to think he wants to convert some arbitrary health value to stars. Although it is relatively easy to show percentage as stars.

Percentage values are better presented by a bar IMHO.

1 Like

Yeah i agree. When I made my health bar I used a bar to represent 10 points and just went by 10% increments.

OP, if you want to do %, you’ll probably have to end on 1% instead of 0% and each bar is 33%, although I’m pretty sure you’re talking about a 3 lives kinda thing

float healthPercentage = Mathf.Clamp01((float)currentHealth / maxHealth);
int numberOfHeartsFull = Mathf.FloorToInt(hearts.Length * healthPercentage);

// keeps one heart showing until actually 0 health
if (healthPercentage > 0) {
    numberOfHeartsFull = Mathf.Max(numberOfHeartsFull, 1);
}

for(int i = 0; i < hearts.Length; i++)
{
    hearts[i].sprite = i < numberOfHeartsFull ? fullHeart : emptyHeart;
}

This is based on some code I used in the past for a health bar. Should work for any arbitrary health values.