Hi, I created a healthbar and I want it to follow the player, this is my script : Code (CSharp): using UnityEngine; using System.Collections; public class SpeechFollow : MonoBehaviour { public GameObject Player; void Start() { } void Update() { //gameObject follow the Player } }
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.
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): //First I create 2 variables public GameObject healthBar; //To get the Slider Information, and Position public float healthBarCorrection; //To correct the position of the healthbar public void Start() { healthBar.GetComponent<Slider>().minValue = 0; //the minimal value of the slider healthBar.GetComponent<Slider>().maxValue = hp;//the maximal value of the slider healthBar.GetComponent<Slider>().value = currentHp; the value of the slider when the monster pop. // we need to instantiate the value of the bar } public void Update() //we need to update each seconds the position of the bar, and the value of the slider { Vector3 monsterPosition = new Vector3(transform.position.x, transform.position.y + healthBarCorrection, transform.position.z); // we need to correct the position of the bar 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. healthBar.GetComponent<Slider>().value = currentHp; } 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 ...
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 ...
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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthFollowMonster : MonoBehaviour { public GameObject healthBar; public GameObject canvas; public GameObject instance; public float healthBarCorrection; public Vector3 monsterPosition; public Vector3 barPosition; // Use this for initialization void Start () { canvas = GameObject.Find("Canvas"); if (!healthBar) { healthBar = Resources.Load<GameObject>("Prefabs/HealthBar"); //Chopper le prefab dans les fichiers } instance = Instantiate(healthBar); instance.transform.SetParent(canvas.transform, false); monsterPosition = new Vector3(transform.position.x, transform.position.y + healthBarCorrection, transform.position.z); instance.GetComponent<Slider>().minValue = 0; instance.GetComponent<Slider>().maxValue = GetComponent<EnemyScript>().hp; instance.GetComponent<Slider>().value = GetComponent<EnemyScript>().currentHp; instance.transform.position = Camera.main.WorldToScreenPoint(monsterPosition); } public void HealthBar() { float t_hp = GetComponent<EnemyScript>().currentHp; monsterPosition = new Vector3(transform.position.x, transform.position.y + healthBarCorrection, transform.position.z); instance.GetComponent<Transform>().position = Camera.main.WorldToScreenPoint(monsterPosition); instance.GetComponent<Slider>().value = GetComponent<EnemyScript>().currentHp; if (t_hp == 0) { Destroy(healthBar); } } // Update is called once per frame void Update () { HealthBar(); } } 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): public void DestroyHealthBar() { Destroy(GetComponent<HealthFollowMonster>().instance); } 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): //Get HealthBar Script if the life is shown HealthFollowMonster t_hfm = GetComponent<HealthFollowMonster>(); if(t_hfm != null) { hfm = t_hfm; } if (t_hfm == null) { hfm = null; Debug.Log("No Health bar attached"); } 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
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
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
Yeah, Unity3D College is a great YouTuber to follow. Very good programmer who understands Unity in great detail.