- Home /
I need some help with a Fractional Heart System?
I'm trying to create a heart system that displays my health with 5 hearts and reduces in fractions every time. I want to make the maximum health 200 so that means every quarter is 10 and every heart is 40.
I know that you have to reference 5 different heart sprites by using 5 cases and assigning each one then when generating them you just use a prefab to spawn them in a horizontal layout group, but that's as far as my knowledge goes, and it's more I know how it works but not how to code it.
Thank you in advance for any help I get:)
Answer by Bunny83 · May 10 at 11:52 PM
You mean a system like this? Over there I made a WebGL example.
That is sort of the system I want, But it's More Where I have 5 hearts throughout the whole game and each heart stores 40 health and 10 for every section of the heart. I've made 5 sprites for the game with different amounts of the heart missing: Full, Three-quarters, Half, Quarter, Empty. I followed a tutorial before on how to make a system, but it adds more hearts every time and calculates the hearts in an annoying way and my brain isn't big enough to program a new version of it. I understood the part where he used a switch statement and enums to switch the heart if it goes into a value from 0 to 4, but I don't understand how to program the drawing of the hearts and updating it when hurt. I know the basic principle, but I'm not able to program it on my own.
[1]: /storage/temp/196173-healthexample.jpg
It should all work in fundamentally the same manner, but you would just apply one more multiplication/division to account for your fraction-of-a-fraction heart values.
int healthPerPartialHeart = 10;
int partsPerHeart = 4;
// ...
int fullHeartCount = hp / (healthPerPartialHeart * partsPerHeart);
int spriteIndex = (hp / healthPerPartialHeart) % partsPerHeart;
// Example: 200 hp
// fullHeartCount(5) = hp(200) / (10 * 4) = 200 / 40 = 5
// irrelevant sprite index at full health
// spriteIndex(0) = (hp(200) / 10) % 4 = 20 % 4 = 0
// Example: 134 hp
// fullHeartCount(3) = hp(134) / (10 * 4) = 134 / 40 = 3
// spriteIndex(1) = (hp(134) / 10) % 4 = 13 % 4 = 1
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to Make Health Decrease Over Time 2 Answers
Player health dropping far to fast 1 Answer
How to make my health decrease? 1 Answer
progress bar 1 Answer