updating visual changes to player health

Hi guys. I have a question. I have made a healthbar scenter code hereript using JavaScript. However, even though my debuglog shows my damage taken and I can see visual changes when I set my enemy damage really high, how can I edit my script to display damage taken over time, not just display 0hp when the playerhealth is empty?

I will attach my script below:

#pragma strict
var hpBar : GUIStyle;
var bgimage : Texture2D;
var fgimage : Texture2D;

static var hpDamage : int = 3;

static var playerHealth = 100.0;

function Update(){
}
function Start(){
}

function OnGUI(){
GUI.BeginGroup(Rect(10,10,256,32));
GUI.Box(Rect(0,0,256,32)bgimage,hpBar);
GUI.BeginGroup(Rect(0,0,playerHealth*256,32));
GUI.Box(Rect(0,0,256,32),fgimage,hpBar);
GUI.EndGroup();
GUI.EndGroup();
}

static function Enemy1Attack1(){
playerHealth -= hpDamage;
}

can you edit your question instead of posting a comment? Also can you format your code in the question, so it's readable? If so I can probably answer your question :)

you're welcome :)

@Seth-Bergman Damn having Karma is cool :P @paradoxxx GUI.BeginGroup does nothing with the width, it only changes the origin, so doing the playerhealth width thing with the GUI.Box is the way :)

1 Answer

1

In other words… you are over-complicating this… All you really need is:

function OnGUI(){
GUI.Box(Rect(0,0,256,32),bgimage,hpBar);
GUI.Box(Rect(0,0,playerHealth*2.56,32),fgimage,hpBar);
}

I also added the decimal (2.56 rather than 256) to fix the scale…

Here’s the result:

http://dl.dropbox.com/u/11203897/HealthBarExample.html

Thank you! Figured it was something small. I will test it out when I get home.. Its an ordeal trying to type a script formatted from an android phone, muchless, getting my phone to accurately recognize text fields.

Ok I made the suggested changes. After a delay, the health bar shrinks into the upper left corner. How can I make it so it only is reduced from the right side of the bar?

@paradoxxx Can you post a screenshot of what happens? The code works like it should, maybe it's something else?