Implementing a linear health bar
In this recipe, we are going to create a linear health bar. The health of the player will be displayed as a bar that shortens when the player's health decreases, and extends with the player's health increases. To achieve this, we will use the Image (Script) component in a different way than what we have in previous recipes, and develop a script to manage the length of the bar.
How to do it...
- First of all, we need to create our health bar, so let's open a graphic program and create it. We can create something that should looks like the following:
- Then, import it in to our project. If the project isn't set to 2D, remember to set the Texture Type of the imported image to Sprite (2D and UI), and then click on Apply.
- Next, create a new Image. Right-click on the Hierarchy panel and then UI | Image, and rename it to Linear Healthbar.
- Inside the Image (Script) component, we have to change Image Type into Filled. The component should change a little...