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): using UnityEngine; using System.Collections; public class PlayerHealthBar : MonoBehaviour{ // textures public Texture2D healthBackground; // back segment public Texture2D healthForeground; // front segment public Texture2D healthDamage; // draining segment public GUIStyle HUDSkin; // Styles up the health integer //values private float previousHealth; //a value for reducing previous current health through attacks private float healthBarWidth; //a value for creating the health bar size private float myFloat; // an empty float value to affect drainage speed public static float curHP; // current HP public static float maxHP; // maximum HP void Start () { curHP -= 0; // drain the current HP to test the health (Assign a value to drain the health) previousHealth = maxHP; // assign the empty value to store the value of max health healthBarWidth = 100f; // create the health bar value myFloat = (maxHP / 100) * 10; // affects the health drainage } void Update(){ adjustCurrentHealth(); } public void adjustCurrentHealth(){ /**Deduct the current health value from its damage**/ if(previousHealth > curHP){ previousHealth -= ((maxHP / curHP) * (myFloat)) * Time.deltaTime; // deducts health damage } else { previousHealth = curHP; } if(previousHealth < 0){ previousHealth = 0; } if(curHP > maxHP){ curHP = maxHP; previousHealth = maxHP; } if(curHP < 0){ curHP = 0; } } void OnGUI () { int posX = 10; int posY = 10; int height = 15; float previousAdjustValue = (previousHealth * healthBarWidth) / maxHP; float percentage = healthBarWidth * (curHP/maxHP); GUI.DrawTexture (new Rect (posX, posY, (healthBarWidth * 2), height), healthBackground); GUI.DrawTexture (new Rect (posX, posY, (previousAdjustValue * 2), height), healthDamage); GUI.DrawTexture (new Rect (posX, posY, (percentage * 2), height), healthForeground); HUDSkin = new GUIStyle(); if(curHP == maxHP){ HUDSkin.normal.textColor = Color.green; HUDSkin.fontStyle = FontStyle.BoldAndItalic; HUDSkin.fontSize = 16; GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin); } else if(curHP < maxHP){ if(percentage <= 50 percentage >= 25){ HUDSkin.normal.textColor = Color.yellow; HUDSkin.fontStyle = FontStyle.BoldAndItalic; HUDSkin.fontSize = 16; GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin); } else if (percentage < 25){ HUDSkin.normal.textColor = Color.red; HUDSkin.fontStyle = FontStyle.BoldAndItalic; HUDSkin.fontSize = 16; GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin); } else { HUDSkin.normal.textColor = Color.white; HUDSkin.fontStyle = FontStyle.BoldAndItalic; HUDSkin.fontSize = 16; GUI.Label(new Rect(30, 28, 100, 50), (int)(previousHealth) + "/" + maxHP.ToString(), HUDSkin); } } } }
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): else { previousHealth = curHP; } if(previousHealth < 0){ previousHealth = 0; } Hint: a webplayer would good idea to demonstrate the health bar damage animation.
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 !
You've to put an "&&" between the two conditions: Code (CSharp): if(percentage <= 50 && percentage >= 25)
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.
... 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.