Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Enemy health appear/disappear when hit, and how to make individual bars for each enemy.

Discussion in 'Scripting' started by Pocknell-r251, Jun 23, 2020.

  1. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class NPCHealth : MonoBehaviour
    7. {
    8.     public Slider enemyHealthBar; // Slider used to keep track
    9.     private EnemyHealth enemyHealth; // Reference to enemy health script where maxhealth and current are
    10.     public bool enemyHurt; // how the game can tell if the enemy is being hit
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         enemyHealth = FindObjectOfType<EnemyHealth>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (enemyHealth.CurrentHealth < enemyHealth.MaxHealth) // if the enemy health is below its max health
    22.         {
    23.             enemyHurt = true;
    24.         }
    25.  
    26.         if (!enemyHurt)
    27.         {
    28.             enemyHealthBar.gameObject.SetActive(false); // why doesn't this work to have the bar not shown when the enemy is not being hurt
    29.         }
    30.  
    31.         if(enemyHurt)
    32.         {
    33.             enemyHealthBar.gameObject.SetActive(true); // why does this not work to only show the bar when the enemy is hurt
    34.             enemyHealthBar.maxValue = enemyHealth.MaxHealth;
    35.             enemyHealthBar.value = enemyHealth.CurrentHealth;
    36.         }
    37.     }
    38. }
    39.  
    I know some of this is probably not the most efficient way of doing things, but I'm fairly new to the concept of unity and programming in general.
    The desired affect is to make the enemies health bar appear when they are hit and disappear when they are not being hit.
    If anyone can also suggest ways I can make it so each enemy has an individual health bar, rather than all subtracting at once, that would be greatly appreciated as that is my next problem to solve after this one.
    Thnx xx
     
  2. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    I have figured something out

    Code (CSharp):
    1. private Canvas NPCView;
    2.  
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         NPCView = GetComponent<Canvas>();
    7.         enemyHealth = FindObjectOfType<EnemyHealth>();
    8.     }
    So now there is NPCView = true and NPCVIew = false with the .enabled function, which makes it so that the bar is not visible when it is not being hit. Now to find a way for it to dissapear again lol, and I still need to find a way to make it individual

    Code (CSharp):
    1. if (!enemyHurt)
    2.         {
    3.             NPCView.enabled = false;
    4.         }
    5.  
    6.         if(enemyHurt)
    7.         {
    8.             NPCView.enabled = true;
    9.             enemyHealthBar.maxValue = enemyHealth.MaxHealth;
    10.             enemyHealthBar.value = enemyHealth.CurrentHealth;
    11.         }
     
  3. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Hey Pocknell! :)

    let me first ask you a few questions,

    1. What type of game is that, i mean .. is it an 3D or 2D Game?
    2. What type of genre is that game? Sword -> hit -> minus health?

    by that info i can tell you if by any case you maybe need only one health bar which eventualy only needs to get updated depending on the enemy hit or if its a kinda top down hack and slash game, then obv. you will need more healthbars etc etc.. :)

    Also , if you need any type of "live support" i stream 6x a week and i LOVE to help people, so here you go : https://www.twitch.tv/arturnachodude
     
    Pocknell-r251 likes this.
  4. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16
    Hey :) thanks for the link xx

    It's a 2D game, it's very simple and based on a tutorial series I'm following closely, as I've not really made games before. But adding a health bar isn't something the tutorial covers, just something I wanted to try my hand at.
    It is sword > hit > minus health, there are multiple enemies of varying style, so I'd need to be able to vary the health bars per enemy. I still haven't managed to figure out how to do individual health bars unfortunately :(

    I also tried but failed to get it to disappear after a few seconds if the enemy was not being hit by coding this:
    Code (CSharp):
    1. waitTimeCounter -= Time.deltaTime; // refering to a counter that counted down from a wait time to the timer to reset
    2.             if (waitTimeCounter <= 0)
    3.             {
    4.                 enemyHurt = false;
    5.                 NPCView.enabled = false;
    6.             }
    however this just turned the health bar off and hitting the enemy again didn't bring it back.
     
  5. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Hey there!

    So to explain, you can make a new Script for your entitys , lets call it "EntityStats" ,
    add there your health / maxhealth:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EntityHealth : Monobehaviour {
    4.  
    5.     private float health;
    6.     public float Health => health;
    7.  
    8.     private float maxHealth => 100f;
    9.     public float MaxHealth => maxHealth;
    10. }
    Then you need also a Healthbar script:

    Code (CSharp):
    1. using UnityEngine.UI;
    2. using UnityEngine;
    3. using System;
    4.  
    5. [Serializable]
    6. public class EntityHealthBar : MonoBehaviour
    7. {
    8.     [SerializeField] private Image healthBarFill;
    9.     private EntityHealth entityHealth;
    10.  
    11.     private void Start() {
    12.           entityHealth = GetComponent<EntityHealth>();
    13.           UpdateHealthBarFill(entityHealth.MaxHealth, entityHealth.Health);
    14.     }
    15.  
    16.     public void UpdateHealthBarFill(float maxHealth, float actualHealth)
    17.     {
    18.         healthBarFill.fillAmount = maxHealth / actualHealth;
    19.     }
    20. }
    make sure you dont call to Update your Healthbar only when you get hit to save performance,

    you can show and hide the entityHealthBar by doing it with:

    Code (CSharp):
    1.         private void OnCollisionEnter2D(Collision2D other) {
    2.            
    3.         }
    4.  
    5.         private void OnCollisionExit2D(Collision2D other) {
    6.            
    7.         }
     
  6. Pocknell-r251

    Pocknell-r251

    Joined:
    Jun 19, 2020
    Posts:
    16

    thanks for this! Looks like I'd have to make the health bar as an image rather than using my slider, does the .fillAmmount function work for sliders too? Either way I'll test it out thanks so much for the help xx