Help with timebar

I’m not able to finalize the script below. Please, I need objective instructions.

Thanks.

My problems:

  • The bar is not decreases, just count the numbers
  • I would make the countdown more slowly (0.1 * time.deltaTime ever used, and it did not work)
  • How do I know that the count reached “0” in order to transport the player and reset the game?

Script:

var bar : boolean;
var lf = 100; //currentHealth
var mxlf = 100; //maxHealth
var barlf; //length of healthbar

function Update(){
    if (bar == false){
        return;
    }
    else
    lf -= Time.deltaTime * 0.1;//still fast - I need to count time in seconds
}

function Start () {
    barlf = (Screen.width/ 2 /(mxlf / lf));
    bar = false;
}

function OnTriggerEnter (other : Collider) {
        bar = true;
}


function OnTriggerExit (other : Collider) {
        bar = false;
}

function OnGUI (){
    if(bar == false)
    return;
    GUI.Box (new Rect (10, 10, barlf, 20), lf + "/" + mxlf);
    GUI.backgroundColor = Color.yellow;//not working
}

function AdjustCurrentHealth(adj){
    lf += adj;
    if ((lf > 0)  (lf <= mxlf)){
        barlf = (Screen.width/ 2 / (lf / mxlf));
    }
    else
    {
        barlf = 0;
    }
}

I had the exact same problem and its a very easy fix. To make the current health decrease more slowly just change it to a float. All you have to do is add a decimal to the end where you defined the variables.

var lf = 100; //currentHealth
var mxlf = 100; //maxHealth

I don’t quite get your first question. Do you want the bar to decrease as the current health decreases? If so you need to got to the variable barlf and change mxlf / lf to lf / mxlf. To send your player to the game over screen use this:

if(lf == 0){
    Application.LoadLevel("Level Name");
    }

You may also want to add this to your script.

if (lf > mxlf) {
     lf = mxlf;
     }
if (lf < 0) {
     lf = 0;
     }
if (mxlf <= 0) {
     mxlf = 1;
     }

that’s just some code that will keep your life from going into the negatives or above your max life and keep your max life from equalling zero or a negative number.

To get your bar to change color make a variable that says this:

var lfbarstyle : GUIStyle;

Then add the variable to the GUI code:

function OnGUI (){
    if(bar == false)
    return;
    GUI.Box (new Rect (10, 10, barlf, 20), lf + "/" + mxlf, lfbarstyle);
    GUI.backgroundColor = Color.yellow;//not working
}

Then you can go into the inspector, find the script, find the variable lfbarstyle and change the color from there. Hope this helps.

Thanks for the reply, X001

If I understood:

I need to do this do turn the decrase more slowly:

var lf = 100.0; //currentHealth
var mxlf = 100.0; //maxHealth

Yes. Ok, I´ll try.

if(lf == 0){
    Application.LoadLevel("Level Name");
    }

Where in the script I´ve to write this?

if (lf > mxlf) {
     lf = mxlf;
     }
if (lf < 0) {
     lf = 0;
     }
if (mxlf <= 0) {
     mxlf = 1;
     }

and this?

var lfbarstyle : GUIStyle;
function OnGUI (){
    if(bar == false)
    return;
    GUI.Box (new Rect (10, 10, barlf, 20), lf + "/" + mxlf, lfbarstyle);
}

One more question:

  • How cai I do to hide the numbers from the bar? I would like show the bar only.

Thank you very much for this help, X001

I´ve to fix almost everything except:

  • decrease bar size
  • move FPC to the original position e reset the game

Some help, please?

Tx

UP

Please!