- Home /
Make health bar with various levels (boss)
Hi, I'm making a health bar for bosses using a slider, and because those bosses have a large amount of HP, I want that bar have levels, something like this:
https://dl.dropboxusercontent.com/u/108692101/BAR1.png
So, when I damage the boss, the bar depletes:
https://dl.dropboxusercontent.com/u/108692101/BAR2.png
And when that level is gone, the bar reverts its size and one of the spheres dissapear:
https://dl.dropboxusercontent.com/u/108692101/BAR3.png
Every level equals 100 HP, and when it decreases or increases over its limit, its level changes. I need help with this please.
Why don't you make two sliders, one red one yellow and position them on top of each other?
Answer by DNRN · Jan 27, 2015 at 03:45 PM
I don't know what you have tried, or how you have implemented the way the damage is done. But you can create the health bar and sphere as a prefab, and put it in a folder named "Resources" then you can load both the health bar and the sphere runtime:
GameObject bar = Instantiate(Resources.Load("BAR", typeof(GameObject)));
GameObject sphere = Instantiate(Resources.Load("SPHERE", typeof(GameObject)));
Another approach could be to create a public reference to both the bar and sphere form your script as a GameObject, and load them runtime:
var bar = Instantiate(BAR) as GameObject;
var sphere = Instantiate(SPHERE) as GameObject;
And end up something like this: public int Health; public int Levels;
public GameObject BarHolder;
public GameObject Bar;
public GameObject Sphere;
// Update is called once per frame
void Update ()
{
if (Health <= 0)
{
Levels--;
}
if (Health >= 100)
{
Levels++;
}
}
private void RemoveLevel()
{
var bar = Instantiate(Bar) as GameObject;
// This moves the bar to the right place, of the GameObject is placed right
bar.transform.parent = BarHolder.transform;
Destroy(Sphere);
}
private void AddLevel()
{
// TODO same as Remove just
}
When you need to remove the gameobject just call Destory(BAR) or Destroy(SPHERE). Here's some more reading: http://docs.unity3d.com/ScriptReference/Resources.Load.html http://docs.unity3d.com/ScriptReference/Object.Instantiate.html http://docs.unity3d.com/ScriptReference/Object.Destroy.html
What I do is the HP bar access the status script of the enemy, so it can read the health data, and because the slider I'm using works with values from 0 to 1, I divide the HP data with a start value. This is how I work with UI Sliders:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class _bossBar : _hud {
public float hpBar;
public float hpBarLimit;
public Slider hpSlider;
public Text levels;
public _bossStatus bossStat;
// Use this for initialization
void Start () {
hpBar = bossStat.hp;
}
void HP$$anonymous$$inus (){
hpBar -= 150 * Time.deltaTime;
}
void HPPlus (){
hpBar += 150 * Time.deltaTime;
}
// Update is called once per frame
void Update () {
base.Engine();
hpBarLimit = bossStat.hpLimit;
float newValue = $$anonymous$$athf.RoundToInt(hpBar)/hpBarLimit;
hpSlider.value = newValue;
if (hpBar >= bossStat.hp)
HP$$anonymous$$inus();
if (hpBar <= bossStat.hp)
HPPlus();
}
}
I made the player HP bar like that, but I want the boss one be like the example I put at start, with more spheres the bigger amount of HP. It behaves like the $$anonymous$$OF special bar:
Your answer
Follow this Question
Related Questions
Scale UI with distance 1 Answer
How do I create an undertale player health bar in C #? 1 Answer
Destroying UI elements problem 1 Answer