Search Unity

2D UI HealthBar follow Player

Discussion in '2D' started by Orinuvan, Jul 11, 2019.

  1. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    Hi, I created a healthbar and I want it to follow the player, this is my script :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpeechFollow : MonoBehaviour
    5. {
    6.     public GameObject Player;
    7.  
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.          //gameObject follow the Player
    16.     }
    17. }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Why don't you just make the healthbar object a child of the player?
     
  3. JapanDoudou_fr

    JapanDoudou_fr

    Joined:
    Jul 11, 2019
    Posts:
    7
    Your script did nothing. I guess you must create a script that transform the position of the Health bar in UI with the player position.
    Or maybe it's possible to attach a GameObject "HealthBar" to the player...
    I will try the both and I came back.
     
    Orinuvan likes this.
  4. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    It doesnt work like that with UI elements
     
  5. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    I guess but therr is no tutorial on how to do that so I dont know how to do that.. :(
     
  6. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Uh actually, is your canvas on world space?
     
  7. JapanDoudou_fr

    JapanDoudou_fr

    Joined:
    Jul 11, 2019
    Posts:
    7
    Hello ! I tried and I did it, but it works only in my game because we need more information.

    Actually, do you want that the HealthBar only show when the monster is Hit, or do you want that the bar is Always on screen ?

    what is the caracteristic of your camera ?

    In my case, I did a script like that :

    Code (CSharp):
    1. //First I create 2 variables
    2. public GameObject healthBar; //To get the Slider Information, and Position
    3. public float healthBarCorrection; //To correct the position of the healthbar
    4.  
    5. public void Start()
    6. {
    7.         healthBar.GetComponent<Slider>().minValue = 0; //the minimal value of the slider
    8.         healthBar.GetComponent<Slider>().maxValue = hp;//the maximal value of the slider
    9.         healthBar.GetComponent<Slider>().value = currentHp; the value of the slider when the monster pop.
    10. // we need to instantiate the value of the bar
    11. }
    12.  
    13. public void Update() //we need to update each seconds the position of the bar, and the value of the slider
    14. {
    15. Vector3 monsterPosition = new Vector3(transform.position.x, transform.position.y + healthBarCorrection, transform.position.z); // we need to correct the position of the bar
    16.         healthBar.GetComponent<Transform>().position = Camera.main.WorldToScreenPoint(monsterPosition); // we say that the position of the bar is a conversion of the position of the monster in my UI units.
    17. healthBar.GetComponent<Slider>().value = currentHp;
    18. }
    It works fine for me but you must adapt it to yours.
    But it's not perfect. I need to show it when the monster pop, and destroy it when the monster is dead. Or more ...
     
  8. JapanDoudou_fr

    JapanDoudou_fr

    Joined:
    Jul 11, 2019
    Posts:
    7
    I guess that the ultimate script Instantiate a Health bar in a canvas, this instance is linked to the monster that instantiate it, and it update the Health each time the monster is touched. It's a little difficult for me now. but I think about it ...
     
    Orinuvan likes this.
  9. JapanDoudou_fr

    JapanDoudou_fr

    Joined:
    Jul 11, 2019
    Posts:
    7
    I did it ! I create a good script :) ! It show you a Health bar that follow the enemies and it's destroyed when the monster die.
    You need some prerequisite :
    A folder "Resources" in your assets that contain a folder ''Prefabs"
    You need to create a GameObject Slider in your main Canvas, your Health bar, and drag it in this Prefabs folder, and name it HealthBar.

    Here is the script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthFollowMonster : MonoBehaviour {
    7.  
    8.     public GameObject healthBar;
    9.     public GameObject canvas;
    10.     public GameObject instance;
    11.     public float healthBarCorrection;
    12.     public Vector3 monsterPosition;
    13.     public Vector3 barPosition;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         canvas = GameObject.Find("Canvas");
    18.  
    19.         if (!healthBar)
    20.         {
    21.             healthBar = Resources.Load<GameObject>("Prefabs/HealthBar"); //Chopper le prefab dans les fichiers
    22.         }
    23.  
    24.         instance = Instantiate(healthBar);
    25.         instance.transform.SetParent(canvas.transform, false);
    26.         monsterPosition = new Vector3(transform.position.x, transform.position.y + healthBarCorrection, transform.position.z);
    27.         instance.GetComponent<Slider>().minValue = 0;
    28.         instance.GetComponent<Slider>().maxValue = GetComponent<EnemyScript>().hp;
    29.         instance.GetComponent<Slider>().value = GetComponent<EnemyScript>().currentHp;
    30.         instance.transform.position = Camera.main.WorldToScreenPoint(monsterPosition);
    31.     }
    32.  
    33.     public void HealthBar()
    34.     {
    35.         float t_hp = GetComponent<EnemyScript>().currentHp;
    36.         monsterPosition = new Vector3(transform.position.x, transform.position.y + healthBarCorrection, transform.position.z);
    37.         instance.GetComponent<Transform>().position = Camera.main.WorldToScreenPoint(monsterPosition);
    38.         instance.GetComponent<Slider>().value = GetComponent<EnemyScript>().currentHp;
    39.         if (t_hp == 0)
    40.         {
    41.             Destroy(healthBar);
    42.         }
    43.     }
    44.  
    45.     // Update is called once per frame
    46.     void Update () {
    47.         HealthBar();
    48.     }
    49. }
    You need th attach the script to your monster. You must rename the "EnemyScript" in you script that rules the enemy life and get the right values in this script.
    In this script, you need to add Something like this

    Code (CSharp):
    1. public void DestroyHealthBar()
    2. {
    3. Destroy(GetComponent<HealthFollowMonster>().instance);
    4. }
    and use the method when the hp of the monster is <= to 0.

    To avoid any error, you must add the the Start method this :

    Code (CSharp):
    1. //Get HealthBar Script if the life is shown
    2.         HealthFollowMonster t_hfm = GetComponent<HealthFollowMonster>();
    3.         if(t_hfm != null)
    4.         {
    5.             hfm = t_hfm;
    6.         }
    7.         if (t_hfm == null)
    8.         {
    9.             hfm = null;
    10.             Debug.Log("No Health bar attached");
    11.         }
    and add a private HealthFollowMonster hfm;
    If there is no HealthFollowMonster attached to the monster, the debug console will show you a Debug.Log and not an error.

    Here is a video of what it looks like in my game :



    Bye bye
     
    Last edited: Jul 14, 2019
    Orinuvan likes this.
  10. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    Ok
    Ok so Im doing a gladiator game, so i need it to always show on the screen, and my canvas is set to camera fit , idk if its any problem with that, its like an arcade game
     
  11. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    You put a lot on work to help here, and I do apreciate that! U rock! But, I need it to actually fit the things I did now, I made an UI Image as a health bar, and I want it to stay on the players head like to stick there , and just follow him, like a child , but Idk why this is so difficult, It should be easy, I really need an 2d side scroller tutorial
     
  12. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    Ur game is awesome btw
     
  13. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
  14. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    THANKS DUDE it worked!!!
     
    Cornysam likes this.
  15. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Yeah, Unity3D College is a great YouTuber to follow. Very good programmer who understands Unity in great detail.
     
    Orinuvan likes this.