Search code examples
c#unityscript

UnityScript: Update Health Function


The game I'm making has a character and enemy. When the character gets hit is closes health. If the character can avoid getting hit for 10 Seconds it regains 5 points of health. If another 10 seconds pass and he still does not get hit he gains another 5 points and this keeps on happening until the character regains full health. If the character get hits before the 10 seconds are completed he loses health and the time starts over.

Currently when I run the C# script it does everything except keep giving health to the player. If I get hit it records the time. The 10 seconds pass and it gives me health but after that the Update function does not continue to give me health it just does it once. I think my logic somewhere is not correct. What you guys think ??

public Slider healthBar;
public float HitResetTimer; //Resetsthetimerto0whenihavebeenhitwithinthe10seconds;
bool beenHit; //Return true or false if ihave been hit
float hitTimer; //keeps track of time from 0 to 10 if i have not been hit

void Awake()
{
    beenHit=false; //player has not been hit so= false
    hitTimer=0.0f; //Set to 0 because i have not been hit yet so no time needs to be registered
}

void OnTriggerEnter(Collider other)
{
    healthBar.value-=10;//If enemy hits character loses 10 points
    beenHit= true; //Is set to true because i have been hit
    hitTimer= HitResetTimer;//If cube hits something hitTimer is no longer time that has occured but reset to 0

}

void RestoreHealth()
{
    if(!beenHit&& healthBar.value<100)
        healthBar.value+=2;
}

//Usethisfor initialization
void Start () {

}

//Updateiscalledonceper frame
void Update () {

    if(beenHit)
    {
        hitTimer -=Time.deltaTime;

        if(hitTimer<0)
        {
            beenHit = false;
            RestoreHealth();
        }
    }

}

Solution

  • In your Update method:

    if(hitTimer<0)
    {
        beenHit = false;
        RestoreHealth();
    }
    

    Take RestoreHealth(); out of all ifs.

    void Update () {
    
        if(beenHit)
        {
            hitTimer -=Time.deltaTime;
    
            if(hitTimer<0)
            {
                beenHit = false;
            }
        }
     RestoreHealth();
    }
    

    The point is you check beenHit in order to reduce hitTimer. Once hitTimer < 0 you set beenHit = false and do RestoreHealth();. After that you have beenHit = false and an Update method having all inside if(beenHit) which gets false state always. So your code never reaches RestoreHealth(); until you set beenHit = true in OnTriggerEnter() which in turn resets timer and you wait again 10 seconds.


    What about checking time difference between current time and time that character got last hit.

    float lastHit; // Holds last time character got hit
    
    void Awake()
    {
        lastHit = Time.time;
    }
    
    void OnTriggerEnter(Collider other)
    {
        healthBar.value -= 100;//If enemy hits character loses 10 points
        lastHit = Time.time;
    }
    
    void RestoreHealth()
    {
        if(healthBar.value<100)
            healthBar.value+=2;
    }
    
    //Updateiscalledonceper frame
    void Update () {
    
        if (Time.time - lastHit > 10) // 10 seconds
        {   
            RestoreHealth();
        }
    }
    

    Note: You should change RestoreHealth() method to give health with specific delay (say 1 second) not on every frame. Since Update will call it every frame after matching your conditions.