- Home /
Health Bar common effect
So this is kind of hard to explain, but it's a very common effect. Basically when a character takes damage the health bar length lowers and there is this other bar under it which fades into the position that the new health bar length is.
For example..
Unity doesn't allow me to create 2 GUI.Boxes in the same OnGUI function so how can I achieve this effect? Please help!
Unity definiely does allow that, so not sure what your issue is maybe post the relevant GUI.Box code
I was trying to call two GUI.Boxes in the same exact location, the first Box would go through just fine but the second one would call an error as follows
"Assets/Scripts/Health.js(29,17): BCE0023: No appropriate version of 'UnityEngine.GUI.Box' for the argument list '(UnityEngine.Rect)' was found."
I read somewhere that I should use GUI.DrawTexture but I'm still currently fiddling around with it, I'm close though!
Answer by Jeff-Kesselman · May 10, 2014 at 11:41 PM
You can do this by creating two GUITextures
The first is the empty bar. The second is the filling. Make the filling of a size to fill the bar, and then scale it at run time to make it smaller.
http://docs.unity3d.com/Documentation/Components/class-GuiTexture.html
Answer by XavierDC · May 11, 2014 at 02:55 AM
I finally figured it out after reading through
http://forum.unity3d.com/threads/193247-Free-Health-Bar-script
my script now goes as follows for JS:
var curHP : int; var Health : int = 100; var maxHealth : int = 100; var healthLength : float; var healthunder : Texture; var healthover : Texture; var damping : float 10.0;
function Start () { healthLength = Screen.width/4; }
function OnGUI () { var previousAdjustValue : float = (curHP healthLength) / maxHealth; var percentage : float = (Health healthLength)/maxHealth;
GUI.DrawTexture (Rect(10,10, (previousAdjustValue), 20), healthover);
GUI.DrawTexture (Rect(10,10, (percentage), 20), healthunder);
}
function Update () {
if (curHP > Health) {
curHP -= ((maxHealth / Health) * (damping)) * Time.deltaTime;
} else if (curHP < Health) {
curHP = Health;
}
if (Health <= 0) {
Death();
Health = maxHealth;
}
if (Health > maxHealth) {
Health = maxHealth;
}
if (maxHealth < 1) {
maxHealth = 1;
}
AdjustCurrenthHealth(0);
}
function AdjustCurrenthHealth (adj : int) { Health += adj;
healthLength = (Screen.width / 4) * (Health / maxFloat);
}
if anyone is having the same problem I hope this helps!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
c# not going to destination on 0 hp 1 Answer
this is part of my health script it gives me a error unknown identifier GameScore pls help thxxx 0 Answers
How can I change this script to use GUITexture instead of GUI number, for health display? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers