- Home /
Health / progression bar
Hi there, i want to create a Health bar (bar that decreases when the players health goes down) I had a look around the forums and answers and found this code:
var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;</p>
function OnGUI()
{
// draw the background:
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function Update()
{
// for this example, the bar display is linked to the current time,
// however you would set this value based on your desired display
// eg, the loading progress, the player's health, or whatever.
barDisplay = Time.time * 0.05;
}
Which creates a bar that grows with time. However i want it to decrease with the players health. In the comments inside the Function Update it says that i need to replace the 'Time.time' with the appropriate command though i cannot figure out how to link it to the players health. I have tried the 3D Platformer tutorial but it starts to confuse me even more as it seems a lot more complex.
Here is the script that gives my player health:
var health = 10;
function ApplyDamage(damage : int) {
health -= damage;
if(health <= 0) {
Die();
}
]
function Die () {
//Respawn Player if wanted.</p>
Destroy(gameObject);
}
This is for my college final major project so would love any help, thank you!
Answer by duck · May 15, 2010 at 09:20 PM
Sounds like you basically need to read about how to access variables and functions on other objects.
It's a bit of fundamental knowledge that you'll need to get to grips with before being able to do much else in unity scripting at all.
Thanks for the link i'll go have a read.
I have got further with this though. i merged the too scripts so that the health variable was also in the health bar script, and changed 'barDisplay = Time.time * 0.05;' to barDisplay = health;' which works, however it only changes once the player has died, ins$$anonymous$$d of going down in progression. Any idea how i can get it to do that? ( i will go read the link though, thanks!)
Ah no worries i managed to get it to work! Thank you so much.
Wait, i am late how'd you get the bar to go progressively down?