Search Unity

How to make a bar go down in seconds>

Discussion in '2D' started by MarcoE73, Apr 26, 2019.

  1. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    Hey so im making a ocean game and I want one bar under the health bar to represent the air that a player has. so I want the bar to go down every second starting from 5 mins and once a person picks up air canisters that I have in the game that air bar goes back to 5 mins. I also want it when if the bar reaches 0 then the player starts taking 5 damage. I tried doing it but I can't figure it out. So far this is how ive tried.

    Code (CSharp):
    1. public class AirCanisterScript : MonoBehaviour
    2. {
    3.     Image airBar;
    4.     public static float health;
    5.     public float damage = 5f;
    6.  
    7.     float timeStart = 300.0f;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         airBar = GetComponent<Image>();
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.  
    20.         airBar.fillAmount = timeStart -= Time.deltaTime;
    21.         if (timeStart <= 0)
    22.         {
    23.             HealthBarScript.health -= damage;
    24.             Player.health -= damage;
    25.         }
    26.     }
    27.  
    28.     void OnTriggerEnter2D(Collider2D collision)
    29.     {
    30.         if (collision.gameObject.tag.Equals("Player"))
    31.         {
    32.             timeStart = 300.0f;
    33.         }
    34.     }
    35. }
     
  2. Cathal111

    Cathal111

    Joined:
    Jan 5, 2017
    Posts:
    14
    Are you using a plain Image for your AirBar or are you using a slider?

    If you're not using a slider, that is what I would recommend. Using a slider, you would use this same code but replace AirBar with a slider, and change the Value property, which would decrease the "fill area" every second
     
  3. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    yeah I think that sounds better I was using a picture so ill give that a try thank you.
     
  4. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    Hey so I got it to work the there's a few problems that I have. one is when the time reaches 0, the fill still has some fill in it instead of showing it empty. the other problem I have is that once the time reaches 0 the player doesn't take any damage. Does anyone know why if I'm accessing the healthbar script and the player script with the health.

    Code (CSharp):
    1. public Slider air;
    2.  
    3.     public static float health;
    4.     public float damage = 5f;
    5.  
    6.     float timeStart = 10f;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         air = GetComponent<Slider>();
    12.  
    13.  
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         Debug.Log("Air " + air.value);
    21.         air.value = timeStart -= Time.deltaTime;
    22.         if (timeStart <= 0)
    23.         {
    24.  
    25.             HealthBarScript.health -= damage;
    26.             Player.health -= damage;
    27.         }
    28.     }
    29.  
    30.     void OnTriggerEnter2D(Collider2D collision)
    31.     {
    32.         if (collision.gameObject.tag.Equals("Player"))
    33.         {
    34.             timeStart = 300.0f;
    35.         }
    36.     }
     
  5. Cathal111

    Cathal111

    Joined:
    Jan 5, 2017
    Posts:
    14
    To fix the slider having some fill left over you should expand the Fill Area, select it's child called "Fill" and set the width of the object to 0, this will remove the problem with having some left in it.

    For it not detecting when you are at 0, I'm not sure as you haven't posted your other classes, however if you put a Debug.Log statement inside the if statement, it will get printed so it is a problem with your other scripts
     
  6. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    Hey I figure it out I just made another if statement stating if the time start is above 0, then the fill is active.

    ill post my script for my healthbar script. this is also giving me trouble because when I collide with the enemy, the bar doesn't go down as it should be. I have the collision on another script called Player and that also calls the health from the healthbarscript just like in the script I posted above where it says "HealthBarScript.health -= damage;" but for some reason it doesn't work. I also know this isn't wrong because I've used before so im not sure why the health bar isn't going down even when the player collides with an enemy.

    Code (CSharp):
    1. public Slider Health;
    2.     Image healthBar;
    3.     public static float maxHealth = 100f;
    4.     public static float health;
    5.  
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         healthBar = GetComponent<Image>();
    11.         health = maxHealth;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.         healthBar.fillAmount = health / maxHealth;
    19.     }
    20.  
     
  7. Cathal111

    Cathal111

    Joined:
    Jan 5, 2017
    Posts:
    14
    Hi, if you change the value of the Slider instead of the filleAmount of the Image it should reduce properly.

    If you do not want to use a Slider and just want to use the image, you must make sure your Image is linked to a sprite, and the Type of the sprite is set to Filled. This will allow you to change the fillAmount
     
    Last edited: Apr 27, 2019
  8. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    okay thank you. I figure out why the slider wasn't working but the slider of the health starts a 0 when I begin the game but it should be 0. ill post my code but its different now from the above code.

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     public static float currentHealth;
    4.     public static float maxHealth = 100f;
    5.     public static int damage = 10;
    6.  
    7.     public Slider Health;
    8.  
    9.     public GameObject gameOverText, restartButton, healthPickup;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         gameOverText.SetActive(false);
    15.         restartButton.SetActive(false);
    16.         maxHealth = currentHealth;
    17.         Health.value = maxHealth;
    18.         Debug.Log(Health.value + "Health" + maxHealth);
    19.  
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.  
    25.     }
    26.  
    27.  
    28.     public void Die()
    29.     {
    30.         gameOverText.SetActive(true);
    31.         restartButton.SetActive(true);
    32.         Destroy(gameObject);
    33.         gameObject.SetActive(false);
    34.  
    35.     }
    36.     public void TakeDamage(int damage)
    37.     {
    38.         //Debug.Log("Hitting = True");
    39.         currentHealth -= damage;
    40.         Health.value = currentHealth;
    41.         if (currentHealth <= 0)
    42.         {
    43.             Die();
    44.         }
    45.  
    46.     }
    47.  
    48.     public void HealPlayer(int heal)
    49.     {
    50.         currentHealth += heal;
    51.         Health.value = currentHealth;
    52.         if(currentHealth >= 100)
    53.         {
    54.             currentHealth = maxHealth;
    55.         }
    56.     }
    57.  
    58.     private void OnCollisionEnter2D(Collision2D collision)
    59.     {
    60.         if (collision.gameObject.tag.Equals("Enemy"))
    61.         {
    62.             TakeDamage(10);
    63.         }
    64.     }
    65. }
    I don't know why the Health.value starts at 0 when the game starts because in this code I made the Health.value = maxHealth which is 100 but it still doesn't work. any ideas why the value of the slider isn't set to 100 when the game starts?
     
  9. Cathal111

    Cathal111

    Joined:
    Jan 5, 2017
    Posts:
    14
    In this code your setting maxHealth = currentHealth, I presume you want it to be currentHealth = maxHealth
     
  10. MarcoE73

    MarcoE73

    Joined:
    Jan 31, 2019
    Posts:
    12
    yes that work lol. one last question. how can I call the function HealPlayer in another script?

    Nvm I got it to work but I have another problem LOL, the problem I have is that once the air reaches 0 the health also goes to zero instead of taking 10 damage little by little like if the player is drowning. ill put the code for the damage.

    Code (CSharp):
    1. public class AirCanisterScript : MonoBehaviour
    2. {
    3.     public Slider air;
    4.  
    5.     public static float health;
    6.     public int damage;
    7.     public static float timeStart = 10f;
    8.     public Player player;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         //Debug.Log("Air " + air.value + "Health :" + Player.health);
    20.         air.value = timeStart -= Time.deltaTime;
    21.  
    22.         if (timeStart <= 0)
    23.         {
    24.             air.fillRect.gameObject.SetActive(false);
    25.             player.TakeDamage(damage);
    26.         }
    27.         else if(timeStart >= 0)
    28.         {
    29.             air.fillRect.gameObject.SetActive(true);
    30.         }
    31.     }
    32. }
     
    Last edited: Apr 27, 2019