Search Unity

Restart wait regen after take damage

Discussion in 'Scripting' started by PixelFireXY, Nov 12, 2015.

  1. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    Hello everyone, I'm trying to create a regen system for the player, everything is fine when the player take damage for the first time, but, when it is taking it for the second time, the regen start again.
    Any ideas? \:
    Thanks in advance <3

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerLifeManagerScript : MonoBehaviour
    5. {
    6.  
    7.     [SerializeField]
    8.     private float playerLife = 50f;
    9.     private static float MAX_PLAYER_LIFE = 100f;
    10.  
    11.     public float regenRate = 1f;       //regen per seconds
    12.  
    13.     private bool isPlayerDead;
    14.  
    15.     private bool takingDamage, newDamage, damageDelay;
    16.  
    17.  
    18.     void Update()
    19.     {
    20.         //if player is NOT taking damage regen life
    21.         if (!takingDamage && playerLife < MAX_PLAYER_LIFE && !isPlayerDead)
    22.         {
    23.             playerLife += regenRate * Time.deltaTime;
    24.         }
    25.     }
    26.  
    27.     //called from enemy
    28.     public void DamagePlayer(float damage)
    29.     {
    30.         //if is the first time just stop the regen, otherwise reset timer on while loop
    31.         if (!takingDamage)
    32.             takingDamage = true;
    33.         else if (!newDamage)
    34.             newDamage = true;
    35.  
    36.         //remove hp
    37.         playerLife -= damage;
    38.  
    39.         //stop all if player is dead
    40.         if (playerLife < 0)
    41.         {
    42.             playerLife = 0;
    43.             isPlayerDead = true;
    44.         }
    45.  
    46.         //if is the first time of damage taken
    47.         if(!damageDelay && !isPlayerDead)
    48.             StartCoroutine(RestoreLife());
    49.     }
    50.  
    51.     private IEnumerator RestoreLife()
    52.     {
    53.         //close the door behind you
    54.         damageDelay = true;
    55.  
    56.         float startTime = Time.time;
    57.  
    58.         //keep loop until the actual time is less than actual time plus 3 seconds
    59.         while (Time.time < startTime + 3)
    60.         {
    61.             //if second attack incoming
    62.             if (newDamage)
    63.             {
    64.                 //reset timer
    65.                 startTime = Time.time;
    66.  
    67.                 //reset variable
    68.                 newDamage = false;
    69.             }
    70.             yield return null;
    71.         }
    72.  
    73.         //otherwise if nothing happen in 3 seconds, start regen again
    74.         takingDamage = false;
    75.         damageDelay = false;
    76.     }
    77. }
    78.  
    79.  
    80. [code/]
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I prefer taking almost everything like this out of Update and control it more directly... a snippet from a health script of mine:

    Code (csharp):
    1.  
    2.  
    3.  
    4. public void Hit(ProjectileInfo info)
    5. {
    6.      CancelInvoke("Regen"); // if hit... cancel any regen
    7.  
    8.      //Do damage stuff
    9.  
    10.      Invoke("Regen", regenAfterDamageTimer);
    11. }
    12.  
    13. public void Regen()
    14.     {
    15.         if ((health < maxHealth))
    16.         {
    17.             health += regenAmount;
    18.             if (health > maxHealth)
    19.             {
    20.                 health = maxHealth;
    21.             }
    22.             else{
    23.                  Invoke("Regen", regenTickTimer);
    24.             }
    25.         }
    26.     }
    27.  
    28.  
     
    AquaAF and PixelFireXY like this.
  3. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    The easier way would probably be using properties, and having the setter cap health to the max. This way you can have the regen run constantly and not care. So long as you don't want to know whether the character is actively regenning (say for a particle effect), this is probably the simplest.
     
  4. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    Thanks, it works like a charm. I was stuck all day to try to solve this problem, I was also uncomfortable about using the Update, because why I need to call regen every frame when I don't need it? And now you found me a solution more easier. Thanks again dude, I appreciate it. =)