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
1
Question by LeviS · Nov 14, 2010 at 06:29 AM · guitextureprogress-barpercentagehealth

Health bar by percentage? (not drawing a box)

Maybe what I am trying to do is not possible. I've done a health bar in the past with an array of images, but I want to learn how to work a health bar based on the player's percentage of health.

Here's a screen shot: alt text

Those are two different images, the border and the actual health bar. I'd like the health bar to shrink towards the left with the percentage of the player's health. Here is what I have at the moment.

// native resolution var nativeVerticalResolution = 1050.0; var nativeHorizontalResolution = 1680.0; var healthBarGUIImage : Texture; //the border image var healthBarImage : Texture; // the actual red bar private var playerInfo : DriverStats; var maxHealth : float; var myHealth : float; var percentHealth : float;

function Awake() { playerInfo = FindObjectOfType(DriverStats); }

function OnGUI () { GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3 (Screen.width / nativeHorizontalResolution, Screen.height / nativeVerticalResolution, 1));

// This draws the border image GUI.Label(Rect (1, nativeVerticalResolution - healthBarGUIImage.height, healthBarGUIImage.width, healthBarGUIImage.height), healthBarGUIImage);

maxHealth = playerInfo.capHealth; // gets the player's maximum health myHealth = playerInfo.health; //get sthe player's current health percentHealth = myHealth / maxHealth; // find the percentage of health that they have

// This draws the actual health bar. GUI.Label(Rect (1, nativeVerticalResolution - healthBarImage.height, healthBarImage.width, healthBarImage.height), healthBarImage); }

I've tried a few things, and haven't got anything that works. I've looked for examples that don't involve arrays of images, but they usually involve just using a box as the health bar, which I don't believe will work with this shape. Is there a simple way to scale this texture down from one side to the other with the GUI?

Thanks!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Eric5h5 · Nov 14, 2010 at 07:39 AM

See here: http://answers.unity3d.com/questions/7449/creating-a-circular-progressbar-timer/7474#7474

Comment
Add comment · Show 1 · 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 LeviS · Nov 14, 2010 at 08:12 AM 0
Share

I read your example already, but while it can probably be altered for my purposes, I don't understand it because there really isn't much there for my to go on. If I had your project, then I could probably figure it out, but its just to big of a leap for me to grasp.

avatar image
0

Answer by Tetrad · Nov 14, 2010 at 07:39 AM

The way we've solved this problem in the past was to not use the GUI system and instead use planes and an ortho camera. From there you can do things like scale the geometry, or use some kind of shader to blend with the material so that it gets progressively more transparent.

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 LeviS · Nov 14, 2010 at 08:13 AM 0
Share

Unfortunately I don't want to use an Orthographic camera :/

avatar image Tetrad · Nov 15, 2010 at 12:51 PM 0
Share

You don't have to have your game use the ortho camera. You can use a separate camera for the UI elements and overlay that on top of your gameplay camera.

avatar image
0

Answer by The_r0nin · Nov 14, 2010 at 02:17 PM

It depends on whether you want the "empty" part of the health bar to be transparent or have some other color. If all you want is the bar to shrink (and missing health be another color), make a parallelogram texture in yellow and scale it on the x-axis based on how much health you are missing. Then, when you put it over the right end of the bar, the red will shrink towards the left and be replaced by yellow. The last bit of health would need to be a separate curved yellow piece to match the left edge of your bar, but at least you'd only have two textures rather than an array.

If you want the missing health part of the bar to be transparent, then you would want to do the same thing as above, but just with a red curve and a red parallelogram, scaled based on how much health you have.

I haven't tried this, but it should work. The only issue might be with the angles of the parallelogram changing when scaled. In that case, you need to use a box to scale and add a curved texture on the right side and a slanted texture on the left.

Hope this helps...

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
avatar image
0

Answer by HROP · Dec 01, 2010 at 11:27 PM

Hi I understand most of the code, however there is one thing I don't get.

How do you create a DriverState type of object, or any type of object for that matter?

I am getting an error "the name Player1 does not denote a valid type ('not found') because I assumed I could just put the name of my player game object instead of DriverState, since my max health, and current health status are being held within a script that is atached to the Player1.

If anyone oculd shine some light on this to me it would be greatly appreciated, as I haven't found much instruction in the Unity scripting reference.

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

No one has followed this question yet.

Related Questions

Health Bar Only For Falling Damage 2 Answers

How do I make a custom health bar? 0 Answers

need help with GUI healthbar 1 Answer

Inverse health bar 2 Answers

What's the best way to create a healthbar which represents the percentage of health the player has left? 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