5SD064

Simplest Health Bar UI

Today, I will be discussing how to create a simple and easy Health bar UI system. This health bar can be used by any Game Object so long as there is a script attached to that object.

Firstly, you want to make sure that you have a Game Object, for example Player, already in your game. Then proceed to add a slider onto your canvas, which is done by Create -> UI -> Slider in the Hierarchy menu and rename it to “HealthBar” for future references. You can then safely de-activate the Handle Slide Area, uncheck the box on the top right (next to the name), as we don’t want players to be able to adjust the health on their own and make sure that in the health bar inspector that Handle Rect is set to None (Rect Transform). Next click on Fill Area and set the fill colour to anything you want your health bar to look like, you can of course drag and drop a sprite for the fill as well. I have 2 fills under Fill Area, I duplicated the original fill and changed the transform, one is the slider fill colours and the other is a sprite next to the health bar which indicates whose health bar it is, basically mine is the face of my player character.

InkedCaptur23e_LI

Crdg.PNG
Figure 1 is the Inspector look on the health bar and Figure 2 is in the Heirachy Menu Under Canvas.

The last process to the health bar, is to add a health script to you Game Object where a public Slider variable will be scripted. This will essentially allow us to drag and drop the slider from the Canvas onto that public variable. The script for the health is also pretty simple and easy to programme as your basically just calculating your current health and setting the slider’s value to that variable.

public Slider healthbar; //in Unity drag and drop the slider onto this public variable
private float MaxHealth {get; set;}
void Start(){
MaxHealth = 100f;
currentHealth = MaxHealth; //resetting health after every completed level
healthbar.value = calculateHealth();// setting the slider's fill to 1 or 100%;
}

void Update(){
healthbar.value = calculateHealth(); //this is basically the slider's current fill number
}

However, to take damage or lose health public function like Takedamage(float damagetaken) needs to be created and then in your collision detection scripts add player.Takedamage(5); see Unity Collision detection and the Collision Layer Technique that works best for more information on that.

Inkedhealthwoefihwef_LI
Figure 3 shows the inspector view of the player game object and the health controller now attached to it.

The slider health bar was chosen because it is quick and easy to set up, it should not take more than 20 minutes however, everyone is different so that value can change. I found out about the slider while i was researching different ways to control my player’s health. There are lots of Unity Tutorials on this as well. At first my team wanted to have heart sprites as health, but since i was still getting used to unity it felt harder for me to do even though it can be as simple as the slider. I then programmed the slider as the health UI and my team decided that that health bar was ok for now and after my team’s Alpha we will change it to a more in-depth health UI system. Of course, the slider is a perfect health bar for any game and it not in any way inferior to other health bar systems.

What makes the Slider Health bar the simplest health UI system is that players can easily recognise exactly what that bar on the side of the screen is because it is the most common form of health UI. This is not the only reason it is the simplest as sprite place holders for health can be confused with other UI sprites you might have in your game and in some ways is more complicated to programme as each sprite would have be set as Active then de-activate every time damage is taken, which makes it tedious in a sense.

Since, my team is working with the agile scrum method, having a slider as a placeholder health UI system is more beneficial as it can then be changed and altered in an agile like sense. To read more about agile scrum, i would recommend the article known as the MDA, it can be found here MDA: A Formal Approach to Game Design and Game Research