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 Bennett-Lynch · Nov 12, 2014 at 06:41 PM · texturetexture2dguitexturefloatingbar

Floating Health Bars

I am attempting to create floating health bars for moving game objects. After a fair amount of Googling, I finally found a decent enough example to start with. The example I found uses GUI.DrawTexture with a normal Texture object (not a GUITexture or Texture2D). Here is the relevant code for the script I have attached to my game objects:

 public class Creep : MonoBehaviour
 {
     public Texture barBG;
     public Texture barFG;
     private Vector3 barPos;
     private float yOffset = 2.2f;
     private float barWidth = 256.0f;
     private float barHeight = 32.0f;
     private float barScale = .25f;
     
     public float maxHealth = 10f;
     public float health;
     private float healthPercent;
     
     private float healthBar;
     private Color highHealth = new Color(7.0f / 255.0f, 161.0f / 255.0f, 7.0f / 255.0f);
     private Color mediumHealth = new Color(161.0f / 255.0f, 157.0f / 255.0f, 7.0f / 255.0f);
     private Color lowHealth = new Color(161.0f / 255.0f, 42.0f / 255.0f, 7.0f / 255.0f);
     private Color barColor;
     
     void Start()
     {
         barWidth *= barScale;
         barHeight *= barScale;
         health = maxHealth;
         healthPercent = health / maxHealth;
     }
     
     void Update()
     {
         barPos = Camera.main.WorldToScreenPoint(new Vector3(transform.position.x, transform.position.y + yOffset, transform.position.z));
         barPos.y = Screen.height - barPos.y;
 
         healthPercent = health / maxHealth;
         if (healthPercent > 0.50f)
         {
             barColor = Color.Lerp(mediumHealth, highHealth, (healthPercent - 0.50f) / 0.50f); // want 1.00 to be 100%, want .50 to be 0%
         }
         else
         {
             barColor = Color.Lerp(lowHealth, mediumHealth, healthPercent / 0.50f); // want .50 to be 100%, want 0.00 to be 0%
         }
     }
     
     void OnGUI()
     {
         GUI.color = barColor;
         GUI.DrawTexture(new Rect(barPos.x - barWidth/2, barPos.y, barWidth, barHeight), barBG, ScaleMode.StretchToFill, true, 0.0f);
         GUI.DrawTexture(new Rect(barPos.x - barWidth/2, barPos.y, barWidth * healthPercent, barHeight), barFG, ScaleMode.ScaleAndCrop, true, 0.0f);
     }
 }

This code does work and it accomplishes most of what I wish to accomplish, but I'm having trouble getting the colors to appear as I would like them to. The colors representing the RGB values I'm using should be bright and vivid. If I create a GUITexture in the game and set the same colors, the green looks like this: alt text

But in-game, using the same texture file (but not as a GUITexture), it looks like this: alt text

Is this because I'm using the GUI.color variable? Is there anyway to brighten it to display the intended RGB color value?

Also, if anyone has any recommendations for a better approach to implementing floating health bars, I'd love to hear it. I tried using GUITextures and Texture2D, but didn't have much luck with them. I'm still new to Unity and its API though, so I would appreciate any advice.

Comment
Add comment · Show 1
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 Kiwasi · Nov 12, 2014 at 08:28 PM 0
Share

Note this whole thing becomes dramatically easier if you use 4.6 and the new UI. Simply attach a world space canvas with a slider and set the value.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zharik86 · Nov 12, 2014 at 08:09 PM

Your code is quite good. And yes, because you using GUI.color. But I would change it a little. I think, you have two textures of bar. One as a background (most likely in dark tones), and the second - full filling in the color (if you want change color in compliance with health as gradient, that color must be white). Further, I will write a simple example:

  public Texture2D barBG; //use Texture2D
  public Texture2D barFG;
  ...
  
  void OnGUI() {
   //First draw background of bar
   GUI.color = Color.white; //resset gui color
   GUI.DrawTexture(new Rect(barPos.x - barWidth/2, barPos.y, barWidth, barHeight), barBG, ScaleMode.StretchToFill);
   //Second draw fill bar, but draw in group
   GUI.BeginGroup(new Rect(barPos.x - barWidth/2, barPos.y, barWidth * healthPercent, barHeight));
   //Apply color
   GUI.color = barColor;
   //Draw bar or him part
   GUI.DrawTexture(new Rect(0, 0, barWidth, barHeight), barFG, ScaleMode.StretchToFill);
   GUI.EndGroup();
  }

I hope that it will help you.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Cutting off a GUITexture's texture 0 Answers

How to make a custom GUITexture at runtime? 2 Answers

GUITexture dimensions constrained to Power of 2? 0 Answers

How to script GUI Texture Window to see it in Script (Object's component) 1 Answer

Best way to show an image 2 Answers


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