Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ouija · Feb 23, 2015 at 11:13 PM · c#uiui imagehealthbarhealth

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);
         }
     }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Feb 24, 2015 at 08:08 PM 0
Share

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)?

avatar image Ouija · Feb 24, 2015 at 08:23 PM 0
Share

yeah you are right, more like the old Doom face is what I wanna go for, good call.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ouija · Feb 24, 2015 at 09:39 PM 0
Share

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?

avatar image Ouija · Feb 24, 2015 at 09:45 PM 0
Share

got the visualcolor added in as well, simple. You rock, Thank again man!

avatar image Mmmpies · Feb 24, 2015 at 09:52 PM 0
Share

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 ;¬)

avatar image
2

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:

https://www.youtube.com/watch?v=y3OZXMxsrUI

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ouija · Feb 23, 2015 at 11:22 PM 0
Share

excellent find, imma watch now see if I can gather knowledge lol

avatar image Ouija · Feb 23, 2015 at 11:34 PM 0
Share

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.

avatar image siaran · Feb 23, 2015 at 11:42 PM 0
Share

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.

avatar image Ouija · Feb 24, 2015 at 01:43 AM 0
Share

@siraran

I tried that method just now, but it gets messy, lots of true and false going on. Its not working proper.

avatar image siaran · Feb 24, 2015 at 01:46 AM 0
Share

...how is it messy/not working? Can you post the relevant code?

Show more comments
avatar image
2

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;
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ouija · Feb 24, 2015 at 08:41 AM 0
Share

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%.

avatar image Polywolf126 · Feb 24, 2015 at 08:55 PM 0
Share

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 ()

avatar image Ouija · Feb 24, 2015 at 09:07 PM 0
Share

NO worries, you replied at least haha Imma toy around with your example , see if I can get something working with what I got

avatar image Polywolf126 · Feb 24, 2015 at 09:35 PM 0
Share

awesome hope things work out :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges