- Home /
First person shooter : Fight night champions dynamic health bar
I was interested in adding a health bar to a game i'm making similar to the one found in the "Fight night" game series more specifically "Fight night : champion". The general idea is to have a health bar which regenerates quickly but slowly depletes in size the longer it stays empty.
This would add challenge to the game as getting hit by a bullet may not have an adverse short term effect because your health regenerates rather quickly but long term speaking your overall health bar will be smaller and you will not be able to absorb as much damage as you did before and will force the player to act more carefully in the future.
I usually prefer to use visuals to illustrate what I mean but unfortunately I can't find anything, other then videos, so I apologize if it is a bit cryptic. I will try to explain further if asked to do so.
So, what have you tried so far? there are lots of health bar tutorials available, so it sounds like you need help with the logic. With some experimentation with CoRoutines, WaitForSeconds and booleans you should quickly get there. Do you have any specific questions regarding what I outlined above?
Well I am a beginner, more or less, so I've watched some tutorials on youtube on how to make a health bar and I figured perhaps that adding a feature that makes the health bar deplete overtime can't realistically be that much extra work to implement.
I just don't know how to do it though, that's the issue, I guess I just need an example to go by that usually seem to help me understand and learn the best.
How would you go about doing this? Which method do you recommend?
So for the health attribute, we need a few values: 1. $$anonymous$$ax health. This is the value that will get lower with replenishing. 2. Current health. Thus is the value that will be replenished continually if below max health. 3. Replenish speed. The time factor that controls how fast the current health replenishes.
With these values, we can make the "theoretical/logical" part of the health system (the practical part would be representing it on the screen with a bar, and needs values for the health bar max and current length).
public float maxHealth = 100f;
public float curHealth = 100f;
public float replenishSpeed = 2.5f;
public int healthBarLength_max = 20;
public int healthBarLength_cur = 20;
void Update() {
if(curHealth < maxHealth) {
curHealth = $$anonymous$$athf.Clamp(curHealth + (Time.deltaTime * replenishSpeed), maxHealth); //so it will never go even slightly above maxHealth
healthBarLength_cur = $$anonymous$$athf.RoundToInt(healthBarLength_cur / healthBarLength_max * curHealth);
//pass this value to your healthBar UI element, depending on how you implement the visual aspect
//updating the length could also be done in the TakeDamage function; it doesn't really need to be updated every frame.
}
public void TakeDamage(float receivedDamage) {
curHealth = $$anonymous$$athf.Clamp(curHealth - receivedDamage, 0);
maxHealth = $$anonymous$$athf.Clamp(maxHealth - receivedDamage, 0);
if(curHealth == 0f) {
//Die!
}
}
}
Your answer
Follow this Question
Related Questions
Health not working! 1 Answer
Empty Health Bar, remove one life 2 Answers
Health Script 1 Answer
Health and ammo GUI not working on re-spawn 0 Answers
Deal damage on collision 2 Answers