Devlog #05 - Implementing Health and Damage System


Introduction:

As per the next plan, now is the time to implement the health and damage system. So, with that, players could do damage with their attacks and also assess their situation in the current state of the battle. So in this dev log, we are going through the development of that aspect of the game.

Designing the Health Bar:

I wanted to put a basic health bar that could conveniently suggest the player's condition without obstructing the screen space too much. I decided to have a health bar that follows the player instead of having a static UI image, so that the players could constantly assess their situation of advantage in the nick of the battle.

So I attached the healthbar component to the ArenaPlayer model and set the position right on top of the player.

Health Bar Positioning
Health Bar Positioning
 

 The health bar basically contains :

1] A background layer for more visibility as the health reduces.

2] A fill image that is made as a slider that gets the value of the current health and changes accordingly

3] A border to encapsulate the borders

4] And a tiny little heart to show that it's a health bar to the player

All kept inside an empty child object called HealthBar

Health Bar Hierarchy
 

Applying Logic to Health Bar:

Since the HealthBar is a slider, we need to apply the logic to control how much it should display. First, we need to add the slider component to the empty child object, which is the health bar, and plug in the fill image to show how much of it is displayed. The code below sets these values and we have SetMaxHealth() that sets maxValue and value, this is done intentionally so that I could call this function during the start and the players would have a full hp bar equal to their max health and everytime they take damage we call SetHealth() to show the health depleting. We also added a gradient so that when it's full HP, they see a green HP bar, and as they are closing to their death, it changes to red, indicating danger. The Evaluate() of the gradient takes values of the range 0 to 1, hence, we used a normalized value to make our value in this range, despite our base max HP being 100, which we constantly keep testing out and changing to adjust the feel of the game.

This script is done in HealthBar.cs

Calculating the health for the health bar:

Here we set the default value and base Damage without any buffs. And once the player gets any buffs, they would be added to the current health and damage. This is done intentionally, so if we want to revert to the base damage at any time, we could do that. And we also bound the key J to simulate the damage taken by the player and to test the proper working of everything.

This script is done in ArenaPlayer.cs

With that, a basic health bar is in place.

Logic behind Damage System:

Same follows to damage's default value or baseDamage, and currentDamage. We then send the current damage to the GunController script, and we further send it to the projectile script, so the projectile now stores the value of the playerCurrentDamage and, on collision, applies the damage to the enemy. For this, all the projectile is tagged as "bullet".

This script is done in ArenaPlayer.cs

Hence, players' health could now be successfully reduced, and once less than or equal to 0, the player dies.

Final Output:

So what's Next?

The next plan is to make all this work as a multiplayer and hence different players can interact with each other and compete.

References:

[1] Unity UI.Slider: https://docs.unity3d.com/2018.2/Documentation/ScriptReference/UI.Slider.html

[2] Unity Gradient: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Gradient.html

Leave a comment

Log in with itch.io to leave a comment.