How to make Health Bar in Unity? | 17 steps

HealthBar is a component of the game. HealthBar represents the status of the gamic character. Therefore, programmers design the HealthBar. This article is for you if you are a game programmer and don’t know how to make Health Bar in Unity.

Also Read this: 6 Steps on How to Delete Projects in Unity? (2021)

17 Steps to make Health Bar in Unity

Follow the below steps and you will know how to make Health Bar in Unity.

  1. The window of Unity is divide into four parts. The Canvas, Project Window, Hairearchy, and Inspector window tab are the component.
  2. Download Unity Image App and Camera Billboard App to your device
  3. Go to the Unity Game Developer window by clicking on the personal.
  4. Select Empty Game Object to clear the previous setting to the default setting.
  5. Select the Create tab from the Hierarchy Window.
  6. Select the Create tab from the Project window too.
  7. Type background in the message box present under the Hairearchy Section.
  8. For the setting of the background, go to the Inspector Window.
  9. Now click on the Add Component to get more settings tools.
  10. The list of the options will drop.
  11. Now, select Sprite Reader from the options.
  12. The tools of Sprite Reader will appear.
  13. Select the search bar to add the White Pixel in the place of None.
  14. Your health bar will appear on the Canvas of the Window.
  15. From the upper case window, you can set Position, Rotation, and size.
  16. From the lower case Window, you can set color, default settings, shapes, and other settings from Transform Tab.

You will have your desire HealthBar on the canvas window. The way you will learn how to make Health Bar in Unity canvas. Meanwhile, you need to know a little more about how to make it functional for the games.

Also Read this: How to join a team on Twitch? | Twitch Tips (2021)

Steps to Make HealthBar Functional for the Games in Unity:

  1. Now, go to the Hierarchy window and type Border in the search bar to make borders.
  2. Now click on the Add Components under the Inspector Window.
  3. The tools will appear set in size, color, and shape of the Border.
  4. The way you will learn how to make HealthBar on Unity and how to Border it.

Steps to Set Script/Code to Support the Health Bar in Unity:

  1. It’s time to add a script on Unity to make HealthBar functional.
  2. Click on the Script option under the Project window.
  3. Ascertain, the Script window appears.

Script to Appear HealthBar 40 % filled:

  1. Start typing under, void smart () {
  2. Type, Trnasform bar= transform. Find (” Bar”);
  3. Then, type, bar.localScale= new Vector3(.4f, 1f);
  4. The way your bar will appear 40% filled.
  5. To confirm, check the canvas window.
  6. Go back to the Script again and start typing under the mentioned Script.
  7. public Class HealthBar: Mono Behaviour. {
  8. And type, Private Transform bar:
  9. Type under public void SetSize(float sizeNormalized) {
  10. Type, Bar.localScale= new Vector3(sizeNormalizes, 1f);

Script/Code to Direct the Health Bar in Unity:

  1. Ascertain you sum up the Script go back to the Project window.
  2. Make a right-click on the script icon to get the menu.
  3. Select Create from the appearing menu.
  4. Now select CR Script from the sub-menu.
  5. Move to the Script again to add a little more data.
  6. Type again, Trnasform bar= transform. Find (” Bar”);
  7. Then, type, bar.localScale= new Vector3(.4f, 1f);
  8. And type, Private Transform bar:

Script/Code to Add the Name in Unity:

  1. Now go to the Script icon again.
  2. And Create an icon named Gamehandler under the Project window.
  3. Move back to the Script again.
  4. Type, [ serialized field ]
  5. Type, Function Periodic . create (()= > {
  6. To add the name of your program type, using XYZ. Utils; just under the using UnityEngine;

Script/Code to Set the Size of Health Bar in Unity:

  1. Go to the Script and add float health= 1f,
  2. Type, if (health>0) {
  3. Now type, health -= .01f;
  4. Type, healthBar. SetSize( health);
  5. Check the canvas to confirm.

Script/Code to Set the Color of Health Bar in Unity:

  1. Get back to the Script again.
  2. type, public void Setcolor(Color = color) {
  3. Now type, bar Find ( “Bar Sprite”). Get Component< ScripteRender>().color=color;

Script/Code to Set Decreasing Health Alert in Unity:

  1. To alert the player about the decreasing health, set the Script to show the current status of health.
  2. Add script if ( health <. 3f) {
  3. Now type, // under 30% health
  4. Type, if ((health* 100f) %3 ==0) {
  5. Type, healthBar. SetColor( Color. white);else {
  6. Add now, healthBar. SetColor( Color. red);
  7. Check the preview in canvas.

As the health drop, the HealthBar blinks in white and red color to alert the player. We are satisfied that we elaborated that how to make Health Bar in Unity.

No Responses

Give a Comment