Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Health bar decrease over time for my script

Discussion in 'Scripting' started by Niborsom, Aug 26, 2019.

  1. Niborsom

    Niborsom

    Joined:
    Oct 7, 2018
    Posts:
    8
    Hello there Unity,

    I've made a healthbar and a script and it seems to work great. However, I want the damage in the slider to decrease over time (say .5 seconds) and show this visually in my healthbar slider. How can I code this, using the below script?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using TMPro;
    5.  
    6. public class HealthMonitor : MonoBehaviour
    7. {
    8.     public float playercurHP;
    9.     public float playermaxHP;
    10.     public float playercurMana;
    11.     public float playermaxMana;
    12.     public TextMeshProUGUI healthText;
    13.     public TextMeshProUGUI manaText;
    14.     public TextMeshProUGUI HPpercentageText;
    15.     public TextMeshProUGUI ManapercentageText;
    16.     public bool isDead = false;
    17.  
    18.     public GameObject playerhealthbarUI;
    19.     public Slider healthbarSlider;
    20.     public Slider manabarSlider;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         playercurHP = playermaxHP;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         //Health
    32.         healthbarSlider.maxValue = playermaxHP;
    33.         healthbarSlider.value = playercurHP;
    34.         healthText.text = playercurHP.ToString("F0") + " / " + playermaxHP.ToString("F0");
    35.  
    36.         //Mana
    37.         manabarSlider.maxValue = playermaxMana;
    38.         manabarSlider.value = playercurMana;
    39.         // F0 and 0 are the same
    40.         manaText.text = playercurMana.ToString("0") + " / " + playermaxMana.ToString("0");
    41.  
    42.         // Mathf.round rounds up the numbers from 0 - ? digits
    43.         //Percentages
    44.         HPpercentageText.text = Mathf.Round (playercurHP / playermaxHP * 100f)  + " %";
    45.         ManapercentageText.text = Mathf.Round (playercurMana / playermaxMana * 100f) + " %";
    46.  
    47.         if (Input.GetKeyDown(KeyCode.Space))
    48.         {
    49.  
    50.             {
    51.                 playercurHP -= 10f * Time.deltaTime;
    52.                 playercurMana -= 10f;
    53.             }
    54.         }
    55.  
    56.         if (playercurMana <= 0)
    57.         {
    58.             playercurMana = 0;
    59.         }
    60.  
    61.         if (playercurHP <= 0)
    62.         {
    63.             playercurHP = 0;
    64.             if (isDead)
    65.             {
    66.                 return;
    67.             }
    68.             Dead();
    69.         }
    70.      
    71.     }
    72.     public void Dead()
    73.     {
    74.         isDead = true;
    75.     }
    76. }
    77.  
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    451
    Split up actual health and visualization.
    Code (CSharp):
    1. playerVisHP = Mathf.Lerp(playerVisHP, playercurHP, Time.deltaTime)
    Note that this is actually a wrong usage of Lerp, but it will do the job for your requirement, namely slowly moving the value from the current visualisation value to the actual value.
    For Slider and text use playerVisHP instead of playercurHP.
     
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.Space))
    2.         {
    3.             {
    4.                 playercurHP -= 10f * Time.deltaTime;  //this confuses me
    5.                 playercurMana -= 10f;
    6.             }
    7.         }
    GetKeyDown executes this code once when the spacebar is depressed. Multiplying by Time.deltaTime would just lead to inaccurate health.

    FlashMuller is right. First, separate the health data and how it's displayed. I'd recommend putting player health in a scriptable object similar to the Unite 2017 talk. Then your UI system is a separate script can see that player health (and max health) and update the slider based on that information. If you don't want an immediate change, you should lerp the slider. But lerp the correct way ;)
     
  4. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    How to lerp:

    https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/


    Code (CSharp):
    1.     void Update() {
    2.         //reset when we press spacebar
    3.         if (Input.GetKeyDown(KeyCode.Space))
    4.             currentLerpTime = 0f;
    5.  
    6.         //increment timer once per frame
    7.         currentLerpTime += Time.deltaTime;
    8.         if (currentLerpTime > lerpTime)
    9.             currentLerpTime = lerpTime;
    10.  
    11.         //lerp!
    12.         float perc = currentLerpTime / lerpTime;
    13.         playerVisHP = Mathf.Lerp(playerVisHP, playercurHP, perc);
    14.     }
     
    FlashMuller likes this.
  5. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    451
    Haha, I was searching for that exact blog post just now. It's brilliant!
     
  6. Niborsom

    Niborsom

    Joined:
    Oct 7, 2018
    Posts:
    8
    Thanks guys, I'll look into it :).

    About
    playercurHP -= 10f * Time.deltaTime;
    I left that in to test the % from yesterday. Ignore that line. Sorry for the confusion.

    Thanks, playerVisHP = Mathf.Lerp(playerVisHP, playercurHP, Time.deltaTime) works for now :) Will seperate health data and use scriptable objects for health etc. later.
     
    Last edited: Sep 22, 2019