Unity UI Tutorial : Health Bar Part 2
In part two of my healthbar tutorial, I will go over how to put health bars over enemies heads in your game. This tutorial uses a lot of scripts and objects from the first part so if you haven't watched that one yet, I recommend going here.
The set up for the scene is very simple. You will not need to import any assets, and can use the default background image for the health bar. However, for final production of your game, I recommend creating your own health bar icons.
The setup for our scene is quite easy. I'll go through top to bottom what you will need.
-Used to render our scene. I chose to rotate the camera 90 degrees in 2D mode so that the camera is facing downwards. This was mostly because of physics stuff, but how you do it in your game will depend on your needs.
-Needed so that you can see objects in your scene. Comes with the hierarchy when you create your scene.
-Makes all of the UI elements visible. Make sure your enemies and player have this attached.
-Just a panel I put in there to make it easier to move stuff around. Not necessary for this tutorial, but may be a good idea for your game.
-UI Image that we will be using as a base for our health. Change the color of it to red, and select one of the default background images for the icon. Also change the image type to filled. The fill method to Horizontal and the fill origin to Left.
-UI element that we will be manipulating with our script. Make all of the same changes that were made in the EmptyHealthImage, but change the color to green.
-Comes with the UI Canvas element. Don't need to do anything with it.
-This is purely cosmetic so that I can show you what is happening in the scene.
-One of Unity's primitive spheres which is colored blue. We will be attaching the HealthBarControl script and movement control script to it so that we can see the health go up and down. Also make sure there is a rigidbody attached with all of the constraints selected.
-Used to show where to instantiate the bullets for the player. Put somewhere in front of player.
-The next two objects are again Unity's primitive cube objects. These will be used to test our add health functionality in our HealthBarDisplay script. Make sure you add a tag to each of them with the string "HealthPack".
-These objects have the same functionality as before, but now they also have their own health. Damage them with bullets. Each enemy should also have the HealthbarDisplay script. Make sure the enemy also has a rigidbody so it can collect collision data.
When you are done, you should end up with a scene looking like the picture below.
Unity Web Player | Health Control Test
« created with Unity »