- Home /
Health Bar Messages
The healthbar works fine. I am not wondering if it is possible for an "image" to pop up next to the health bar? Assume Health will be 100. And there are 5 different images. One image when health is 100, another image when health is 80, another image for 60, another 40, etc.Using the UI because that's how the healthbar is made. Any ideas?
public Image one;
public Image two;
public Image three;
public Image four;
public Image five;
private void HandleHealth()
{
healthText.text = "Health";
float currentXValue = MapValues (currentHealth, 0, maxHealth, minXValue, maxXValue);
healthTransform.position = new Vector3 (currentXValue, cachedY);
if (currentHealth > maxHealth / 2) {
visualHealth.color = new Color32 ((byte)MapValues (currentHealth, maxHealth / 2, maxHealth, 255, 0), 255, 0, 255);
}
else
{
visualHealth.color = new Color32 (255, (byte)MapValues (currentHealth, 0, maxHealth / 2, 0, 255), 0, 255);
}
}
Are the images all the same size and same location, a bit like an old Doom face that got more and more beat up the lower the health or do you want them to appear above health bar at each % point mentioned (20,40,60,80,100)?
yeah you are right, more like the old Doom face is what I wanna go for, good call.
Answer by Mmmpies · Feb 24, 2015 at 09:15 PM
OK you want to have an image and an array of sprites. For ease let's just make the both public so you can just drag on the sprite in the inspector.
public Sprite[] hurtSprites;
public Image hurtImage;
Put an image on the Canvas and drag that onto the public hurtImage slot in the script.
Now we need to deal with the HandleHealth function:
void HandleHealth()
{
if(health < maxHealth / 5)
HealthIcon(0);
else if(health >= maxHealth / 5 && health < maxHealth / 2.5f)
HealthIcon (1);
else if(health >= maxHealth / 2.5f && health < maxHealth / 1.6666f)
HealthIcon (2);
else if(health >= maxHealth / 1.6666f && health < maxHealth / 1.25f)
HealthIcon (3);
else if (health == maxHealth)
HealthIcon (4);
}
so we set the 20%, 40% and 60% and so on with maths, that allows the max health to 100 or 320 or whatever. Note 60% is 1.66 recurring so never gonna be accurate but close enough for what we need.
void HealthIcon(int IconNumber)
{
Debug.Log (hurtSprites.Length + " : " + IconNumber);
hurtImage.sprite = hurtSprites[IconNumber];
}
And this last bit just sets the icon from the public array.
Dude your brain is awesome loll. Just tested the script, that was exactly what I needed. All seems to work perfect, I still need to add the visualhealth.color back tho, should still work the same as before?
got the visualcolor added in as well, simple. You rock, Thank again man!
Hey glad it helped and that you got it working but I already thumbed up both @siaran and @Polywolf126 answers as they are both workable solutions. Feel free to do the same, but you're right I'm great ;¬)
Answer by siaran · Feb 23, 2015 at 11:21 PM
normally I'd tell you that googling for a healthbar tutorial isn't hard, but with the new UI there aren't that many and a lot of them are overly complicated - so here is a good one:
excellent find, imma watch now see if I can gather knowledge lol
just finished the video, it is indeed a simple way to achieve a healthbar. But I already have a working healthbar, my question is, how do I get an image appear once the bar reaches certain percentages. Basically a image pop up next to the healthbar.
Well, one way you can do it is put a UI element with your image next to it, disable the gameobject it's on and do something like
public Image almostDead;
void TakeDamage(float amount){
currentHealth -= amount;
if(currentHealth < maxHealth/4){
almostDead.gameObject.SetActive(true);
}else{
almostDead.gameObject.SetActive(false);
}
}
EDIT: This is just one way to do it, I could think up a few more. But they all come down to: have an image, check health, if it's below some percentage, show it, otherwise, don't show it.
@siraran
I tried that method just now, but it gets messy, lots of true and false going on. Its not working proper.
...how is it messy/not working? Can you post the relevant code?
Answer by Polywolf126 · Feb 24, 2015 at 03:38 AM
this is a code i use it works for the most part and "healthimage" is just an image with filed check.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class enter : MonoBehaviour {
public Camera camera;
CharacterController cc;
public float health = 100.0f;
public Image healthimage;
public Text dead;
public BoxCollider spikes;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController>();
dead.enabled = false;
healthimage.fillAmount = 100.0f;
}
// Update is called once per frame
void Update () {
if (healthimage.fillAmount <= 0.0f)
{
healthimage.fillAmount = 0.0f;
};
healthimage.fillAmount += 0.00053f * Time.deltaTime;
health += 0.5f * Time.deltaTime;
if(health >= 100)
{
health = 100;
}
if (health <= 0.1199999f)
{
health = 0;
cc.enabled = false;
audio.Stop ();
dead.enabled = true;
}
if (healthimage.fillAmount <= 0)
{healthimage.fillAmount = 0;
}
Hey man, thanks for your reply. I already have a health bar that is working, I am looking for a way to show an image every 20% of the fill bar, assu$$anonymous$$g its max health is 100. Assume I make a new health bar using what you provided, how would I make an image appear every 20% of the fill? assu$$anonymous$$g it ma health is 100%.
sorry for the late response but what you would want to do is set the image.fill value accordingly to the health value at 20% increments then make the separate image appear.
for example :
public float health = 100;
public image blood1;
if (health <= 80) { // setting the values healthimage.fillamount = 0.8f; }
if (healthimage.fillamount <0.8) { blood1.enabled = true; }
i hope this is what you were asking.you would first have to set the "blood1" image to false by default in void start ()
NO worries, you replied at least haha Imma toy around with your example , see if I can get something working with what I got
Your answer
Follow this Question
Related Questions
What's wrong with this health bar GUI script? (C#) 1 Answer
How do I expose a health serialize script on a health bar ? 0 Answers
I have an array in Unity. I want to assign an image based on the gameobject's tag 1 Answer
How can I create a UI element and attach my script to it for a functional health bar? 1 Answer
How do I create an undertale player health bar in C #? 1 Answer