Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying To Make Health Buff But It Continues To Add The Buff Over And Over For All 5 Seconds

Discussion in 'Scripting' started by YoungCoder, Nov 19, 2016.

  1. YoungCoder

    YoungCoder

    Joined:
    Oct 10, 2014
    Posts:
    15
    using UnityEngine;
    using System.Collections;

    public class PlayerCore_Stats : MonoBehaviour {

    //HEALTH VARIABLES
    public float maxHlth = 100.0f; //maxHlth
    public float curHlth; //curHlth
    public float minHlth = 0.0f; //minHlth

    //STAMINA VARIABLES
    public float maxStam = 100.0f; //maxStam
    public float curStam; //curStam
    public float minStam = 0.0f; //minStam

    //SANITY VARIABLES
    public float maxSanity = 100.0f; //maxSanity
    public float curSanity; //curSanity
    public float minSanity = 0.0f; //minSanity

    //DYNAMIC BUFF AMOUNTS
    public float staminaBuffAmount;
    public float healthBuffAmount;
    public float sanityBuffAmount;

    // Use this for initialization
    void Start ()
    {
    curHlth = maxHlth;
    curStam = maxStam;
    curSanity = maxSanity;
    healthBuffAmount = 0;
    staminaBuffAmount = 0;
    sanityBuffAmount = 0;
    }

    // Update is called once per frame
    void Update ()
    {
    StaminaCheck ();
    HealthCheck ();
    SanityCheck ();
    giveStaminaBuff (staminaBuffAmount);
    giveSanityBuff (sanityBuffAmount);

    if (healthBuffAmount > 0) {
    giveHealthBuff (healthBuffAmount);
    }
    }
    //================================================================

    //Give A Health Buff
    public void giveHealthBuff (float healthBuffAmount)
    {
    curHlth = curHlth + healthBuffAmount;
    StartCoroutine ("WaitFor_HealthBuffRemoval");
    }

    //Wait To Remove Health Buff
    IEnumerator WaitFor_HealthBuffRemoval ()
    {
    //Wait 120 seconds
    yield return new WaitForSeconds(5);
    healthBuffAmount = 0;
    //Change Stats
    curHlth = maxHlth;
    }

    //================================================================

    //Give A Stamina Buff
    public void giveStaminaBuff (float staminaBuffAmount)
    {
    curStam = curStam + staminaBuffAmount;
    StartCoroutine ("WaitFor_StaminaBuffRemoval");
    }

    //Wait To Remove Stamina Buff
    IEnumerator WaitFor_StaminaBuffRemoval ()
    {
    //Wait 120 seconds
    yield return new WaitForSeconds(120);
    //Change Stats
    curStam = curStam - staminaBuffAmount;
    }

    //================================================================

    //Give Sanity Buff
    public void giveSanityBuff (float sanityBuffAmount)
    {
    curSanity = curSanity + sanityBuffAmount;
    }

    //Wait To Remove Sanity Buff
    IEnumerator WaitFor_SanityBuffRemoval ()
    {
    //Wait 120 seconds
    yield return new WaitForSeconds(120);
    //Change Stats
    curSanity = curSanity - sanityBuffAmount;
    }

    //================================================================

    public void StaminaCheck ()
    {
    //Disable Ability to Run
    if (curStam <= 20.0f)
    {
    //Disable Ability to Run
    //Display UI Message: "Stamina Levels Low"
    Debug.Log("Player Stamina Levels Low");
    }

    if (curStam <= 21.0f)
    {
    //Activate Run
    Debug.Log("Run is Not Available Due To Stamina Lower Than 21.0");
    }
    else
    {
    Debug.Log("Stamina Levels Are Ok");
    }

    /*
    Make seperate if statements for disabling and enabling specific player movement
    based on current Stamina, Health, etc. abilities.
    */
    }

    public void StaminaDecrease (float stamina_decrease)
    {
    curStam = curStam - stamina_decrease;
    }

    //================================================================

    public void HealthCheck ()
    {
    if (curHlth <= minHlth)
    {
    //Kill Player
    //UI Death Screen
    //PlaySound();
    Debug.Log("Player Dead | Health Level Is At 0");
    }

    if (curHlth <= 20.0f)
    {
    //Display UI Message: "Health Levels Low"
    //Add UI FX
    Debug.Log("Health Levels Are Low");
    }
    else
    {
    Debug.Log("Health Levels Are Ok");
    }
    }

    public void Damage (float damageAmount)
    {
    curHlth = curHlth - damageAmount;
    }

    //================================================================

    public void SanityCheck ()
    {
    if (curSanity <= minSanity)
    {
    //Kill Player
    //Could Possibly Add In Other Options Besides Killing The Player
    //Display UI Message "Player Has Reached The Lowest Mental State"
    Debug.Log("Player Sanity Has Reached It's Lowest");
    }

    if (curSanity <= 15.0f)
    {
    //Display UI Message: "Sanity Levels Low"
    //Struggle Attack
    Debug.Log("Player Sanity Levels Are Low");
    }
    else
    {
    Debug.Log("Sanity Levels Are Ok");
    }
    }

    public void SanityDecrease (float sanity_decrease)
    {
    curSanity = curSanity - sanity_decrease;
    }

    //================================================================

    /*private void CheatProtection_Stat ()
    {
    if (curHlth > maxHlth)
    {
    curHlth = maxHlth;
    }

    if (curStam > maxStam)
    {
    curStam = maxStam;
    }

    if (curSanity > maxSanity)
    {
    curSanity = maxSanity;
    }
    }
    */
    }
     
  2. Andenberg

    Andenberg

    Joined:
    Nov 5, 2016
    Posts:
    11
    Try replacing your WaitFor_HealthBuffRemoval coroutine with the one at the bottom of this comment, I'm not 100% sure if this will fix your problem because I have not tested it however I think your problem lies within update() function in your code:

    Code (CSharp):
    1. if (healthBuffAmount > 0)
    2. {
    3.   giveHealthBuff (healthBuffAmount);
    4. }
    And in your coroutine you wait 5 seconds before setting the healthBuffAmount to 0 causing update to apply the buff every frame for the next five seconds.

    Code (CSharp):
    1. //Wait To Remove Health Buff
    2. IEnumerator WaitFor_HealthBuffRemoval ()
    3. {
    4.   healthBuffAmount = 0;
    5.   //Wait 120 seconds
    6.   yield return new WaitForSeconds(5);
    7.   //Change Stats
    8.   curHlth = maxHlth;
    9. }
    Hope this helped let me know if it worked! :)
     
  3. YoungCoder

    YoungCoder

    Joined:
    Oct 10, 2014
    Posts:
    15
    Thank you will do
     
  4. YoungCoder

    YoungCoder

    Joined:
    Oct 10, 2014
    Posts:
    15
    Thank you it fixed it!
     
  5. Andenberg

    Andenberg

    Joined:
    Nov 5, 2016
    Posts:
    11
    Great glad I could help!
     
    YoungCoder likes this.