Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Free Health Bar script

Discussion in 'Made With Unity' started by DavoAp, Jul 29, 2013.

  1. DavoAp

    DavoAp

    Joined:
    Dec 12, 2011
    Posts:
    9
    Hi Guys

    I would like some honest, constructive feedback on what you think of the health bar script I have written some time ago. Asides from that, this script is free for anyone to use as a way of contributing to the forums even though I'm still a beginner/moderate programmer.

    Kind, honest feedback is appreciated, thank you.

    Oh btw if there are questions please do ask and I'll do my best to answer them correctly.

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class PlayerHealthBar : MonoBehaviour{
    7.    
    8.     // textures
    9.     public Texture2D healthBackground; // back segment
    10.     public Texture2D healthForeground; // front segment
    11.     public Texture2D healthDamage; // draining segment
    12.     public GUIStyle HUDSkin; // Styles up the health integer
    13.    
    14.     //values   
    15.     private float previousHealth; //a value for reducing previous current health through attacks
    16.     private float healthBarWidth; //a value for creating the health bar size
    17.     private float myFloat; // an empty float value to affect drainage speed
    18.     public static float curHP; // current HP
    19.     public static float maxHP; // maximum HP
    20.        
    21.     void Start () {
    22.         curHP -= 0; // drain the current HP to test the health (Assign a value to drain the health)
    23.         previousHealth = maxHP; // assign the empty value to store the value of max health
    24.         healthBarWidth = 100f; // create the health bar value
    25.         myFloat = (maxHP / 100) * 10; // affects the health drainage
    26.     }
    27.    
    28.     void Update(){
    29.         adjustCurrentHealth();
    30.     }
    31.    
    32.     public void adjustCurrentHealth(){
    33.                        
    34.         /**Deduct the current health value from its damage**/  
    35.        
    36.         if(previousHealth > curHP){
    37.             previousHealth -= ((maxHP / curHP) * (myFloat)) * Time.deltaTime; // deducts health damage
    38.         } else {
    39.             previousHealth = curHP;
    40.         }
    41.        
    42.         if(previousHealth < 0){
    43.             previousHealth = 0;
    44.         }
    45.        
    46.         if(curHP > maxHP){
    47.             curHP = maxHP;
    48.             previousHealth = maxHP;
    49.         }
    50.        
    51.         if(curHP < 0){
    52.             curHP = 0;
    53.         }
    54.     }
    55.    
    56.     void OnGUI () {
    57.         int posX = 10;
    58.         int posY = 10;
    59.         int height = 15;
    60.                
    61.         float previousAdjustValue = (previousHealth * healthBarWidth) / maxHP;
    62.         float percentage = healthBarWidth * (curHP/maxHP);
    63.                
    64.         GUI.DrawTexture (new Rect (posX, posY, (healthBarWidth * 2), height), healthBackground);       
    65.        
    66.         GUI.DrawTexture (new Rect (posX, posY, (previousAdjustValue * 2), height), healthDamage);
    67.        
    68.         GUI.DrawTexture (new Rect (posX, posY, (percentage * 2), height), healthForeground);
    69.        
    70.         HUDSkin = new GUIStyle();
    71.        
    72.         if(curHP == maxHP){
    73.             HUDSkin.normal.textColor = Color.green;
    74.             HUDSkin.fontStyle = FontStyle.BoldAndItalic;
    75.             HUDSkin.fontSize = 16;
    76.             GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin);
    77.            
    78.         } else if(curHP < maxHP){
    79.            
    80.             if(percentage <= 50  percentage >= 25){
    81.                 HUDSkin.normal.textColor = Color.yellow;
    82.                 HUDSkin.fontStyle = FontStyle.BoldAndItalic;
    83.                 HUDSkin.fontSize = 16;
    84.                 GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin);
    85.        
    86.             } else if (percentage < 25){
    87.                 HUDSkin.normal.textColor = Color.red;
    88.                 HUDSkin.fontStyle = FontStyle.BoldAndItalic;
    89.                 HUDSkin.fontSize = 16;
    90.                 GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin);
    91.            
    92.             } else {
    93.                 HUDSkin.normal.textColor = Color.white;
    94.                 HUDSkin.fontStyle = FontStyle.BoldAndItalic;
    95.                 HUDSkin.fontSize = 16;
    96.                 GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin);
    97.             }  
    98.         }
    99.     }
    100. }
    101.  
    102.  
     
  2. Atmey

    Atmey

    Joined:
    Nov 3, 2012
    Posts:
    88
    I didn't try the script but it seems pretty good, personally ( I don't know if it effects performance) I prefer not to set values on each update when not needed like:
    Code (csharp):
    1.  
    2. else {
    3.  
    4.             previousHealth = curHP;
    5.  
    6.         }
    7.  
    8.        
    9.  
    10.         if(previousHealth < 0){
    11.  
    12.             previousHealth = 0;
    13.  
    14.         }
    15.  
    Hint: a webplayer would good idea to demonstrate the health bar damage animation.
     
  3. joandku

    joandku

    Joined:
    Aug 12, 2014
    Posts:
    3
    I agree
     
    R-Lindsay likes this.
  4. R-Lindsay

    R-Lindsay

    Joined:
    Aug 9, 2014
    Posts:
    287
    Beautifully executed necro. +50 DKP.
     
    WhiteJuky likes this.
  5. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    Good job, its easy to use!

    For me,
    i will change the hp shown to the player to curHp and the bar with color to show curHp (green), previousHealth (yellow) and degrading slowly and missing health (red)

    continue your great work !
     
  6. yuukinikko

    yuukinikko

    Joined:
    Apr 9, 2015
    Posts:
    1
    why do i get an error?...it says unexpected symbol 'percentage' ..help pls
     
  7. Mazinger-M

    Mazinger-M

    Joined:
    May 26, 2015
    Posts:
    1
    You've to put an "&&" between the two conditions:

    Code (CSharp):
    1. if(percentage <= 50 && percentage >= 25)
     
  8. Xenoc1DE

    Xenoc1DE

    Joined:
    Jan 20, 2019
    Posts:
    1
    It should work with a few tweeks some coding wasn't described so the components weren't able to function properly such as the percentage of health but overall was great.
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ... you might not want to use a script for the previous UI system these days... OnGUI was replaced in game with the Canvas based approach a while ago now.
     
  10. centjesnl

    centjesnl

    Joined:
    Jan 16, 2021
    Posts:
    1
    hey i realy like your script but i dont now where i need to put it in if you can help thanks alot
     
  11. jajkovxx

    jajkovxx

    Joined:
    Feb 2, 2022
    Posts:
    1
    how to add this script and where?
     
  12. Shaggy5682

    Shaggy5682

    Joined:
    Mar 25, 2022
    Posts:
    1
    how do i get it to show up above my players
     
  13. njhighley

    njhighley

    Joined:
    May 3, 2022
    Posts:
    2
    How do I connect this to by health script?
     
  14. njhighley

    njhighley

    Joined:
    May 3, 2022
    Posts:
    2
    How do i get it to detect damage?