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 Sousai · Jan 02, 2015 at 12:36 AM · guiguitexturehealthbarhealth

Issue with GUITexture width.

Within my project I have a GUITexture on an object controlled by a scripted that when the value of a characters HP goes down, it is shortened by that percent etc.

Although, the GUITexture works for the most part besides the fact that in order for the texture to be completely gone and or thinned out to where it is un-seen, the W value of the pixel inset must be -8.5. However, that doesn't do me any good because the percentage shouldn't go below 0%.

How would I fix the pixel inset of the GUITexture so that when the W value is 0, the texture isn't visible?

(P.S. As I've created the script so that the bar gradually slides up and down, I'd rather not use an if statement telling the bar to automatically jump to -8.5 because it would be an odd jump and it still would be un-proportional up until that point.)

Below are the current values of the GUITexture when it is in the correct position on the screen and filled to 100%. I am also using GUITexture (Legacy).

alt text

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

1 Reply

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

Answer by george_vasilchenko · Jan 02, 2015 at 01:06 PM

Hey, here is an option to use just a Texture of a health bar and control its size by tweaking width of GUI.DrawTexture method. There is a simple range mapping function used in order to convert health of a player into the size of the texture. Hope it helps.

using UnityEngine; using System.Collections;

public class HUD : MonoBehaviour {

 public  Texture healthBar;                         //your health bar texture
 public  float playerHealth;                         //to hold player's health
 private float healthBarLength;                     //to hold bar length (texture.pixelInset.width)

 private float playerHealth_min    = 0;            //players health range min value
 private float playerHealth_max       = 100;        //players health range max value
 private float healthBarLength_min = 0;             //bar length range min value
 private float healthBarLength_max = 200;        //bar length range max value (desired bar max lenght)

 private string playerHealth_text;                //spit it out on the screen    

 void Start () 
 {
     playerHealth = 100;                    //initialize health to its max value
 }
 
 void Update () 
 {
     //map range of health to range of healthBar length
     healthBarLength = (playerHealth - playerHealth_min) / 
                       (playerHealth_max - playerHealth_min) * 
                       (healthBarLength_max - healthBarLength_min) + healthBarLength_min;

     //constrain values to >= 0
     if (playerHealth <= 0)       playerHealth = 0;
     if (healthBarLength <= 0) healthBarLength = 0;

     //convert health to string
     playerHealth_text = ((int)playerHealth).ToString();

     if (playerHealth <= 25) 
     {
         Debug.Log("LOW HEALTH!");        //your code is here
     }
     if (playerHealth > 25) 
     {
         Debug.Log("WE ARE GOOD!");        //your code is here
     }
 }

 void OnGUI()
 {
     GUI.Label(new Rect(30, 60, 100, 20), "Player Health");
     GUI.Label(new Rect(120, 60, 100, 20), playerHealth_text);

     GUI.DrawTexture (new Rect (30, 90, (int)healthBarLength, 20), healthBar);
 }

}

Comment
Add comment · Show 2 · 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 Sousai · Jan 06, 2015 at 09:28 AM 0
Share

Thank you very much! I'll implement this and play with this a bit.

avatar image george_vasilchenko · Jan 06, 2015 at 11:37 AM 0
Share

Good luck! Cheers!

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

Help With Enemy Health bars 3 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

How do I make a custom health bar? 0 Answers

Healthbar not always working, someone please help! 2 Answers

How to prevent guitexture from vertically shrinking? 0 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