Bleeding and Health Script Problem

Hey guys, I got a little problem :slight_smile: In this script I was trying to create a little health system and the regeneration works great so far :slight_smile: But the bleeding, not so good :confused: Could someone give me a hint on what I can do to make both work the way I want to? Thank you in advance!

var MaxHealth = 80.0;

var health = MaxHealth;
 
//Time Till the Player starts to Regenerate after timer Trigger
var regenTime = 10.0;
//How fast the Player Regenerates
var regenSpeed = 1.0;
 
//Time till the player starts to Bleed after timer Trigger
var bleedTime = 50.0;
//How fast the Player Bleeds
var bleedSpeed = 1.0;
 
//Timer for Regeneration/Bleeding
private var timer = 0.0;
 
function Start () {
 
}
 
function Update () {

    if(health<=0)
       die();
 
    if(health>20 && health<MaxHealth){
       if(timer<regenTime) {
         timer+=0.1;
       }else{
         health += regenSpeed/10;
       }
       }else{
     	timer = 0.0;
     
    if(health<=20 && health>0){
       if(timer<bleedTime) {
         timer+=0.1;
       }else{
         health -= bleedSpeed/10; 
       }
     }else
     	timer = 0.0;  
    }
} 
function die () {
}

just a few comments.
Ideally you should add Time.deltaTime to timer instead of 0.1 so that bleedtime doesn’t vary for different framerates.

you’re using the same timer variable for both regen and health, but regen and health are treating it as if they own it, i.e. both are incrementing and zeroing it causing conflicts.

I’d suggest adding a breakpoint and single stepping so that you can see whats really happening, or at least outputting some values to the console

have fun
Steve

ok Looks like you can’t have multiple comments (I’m new to this)
I’d possibly do something like below, although I’m not sure of the overall aim, but maybe that’s because this is only a fragment of the whole code or are you destined to bleed to death as soon as you’re health drops below 20

anyway code is untested and uncompiled so you may need to tweek it a bit.

Time.deltaTime


var MaxHealth = 80.0;
var BleedThreshold = 20.0;
var RegenThreshold = 20.0;

var health = MaxHealth;
 
//Time Till the Player starts to Regenerate after timer Trigger
var regenTime = 10.0;
//How fast the Player Regenerates
var regenSpeed = 1.0;
 
//Time till the player starts to Bleed after timer Trigger
var bleedTime = 50.0;
//How fast the Player Bleeds
var bleedSpeed = 1.0;
 
//Timer for Regeneration/Bleeding
private var RegenTimer = 0.0;
private var BleedTimer = 0.0;

function Start () {
	RegenTimer = regenTime;
	BleedTimer = bleedTime;
}
 
function Update () {
 
    if(health<=0)
       die();
 
    if(health>RegenThreshold && health<MaxHealth){
       if(RegenTimer>0.0) {
	 RegenTimer-=Time.deltaTime;
       }else{
         health += regenSpeed*Time.deltaTime;
       }
       }else{
        RegenTimer = regenTime;
 
    if(health<=BleedThreshold && health>0){
       if(BleedTimer>0.0) {
         BleedTimer-=Time.deltaTime;
       }else{
         health -= bleedSpeed*Time.deltaTime; 
       }
     }else
        BleedTimer = bleedTime;  
    }
} 
function die () {
}