Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Nallac · May 13, 2010 at 01:31 PM · javascripthealthbarhealth

How Do I Make A Health Bar

I have health bar textures and a working health system but how do i make it into a bar? so how would i change between the images according to how much health is left out of 100??

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 Artifactx · Oct 18, 2014 at 05:34 AM 0
Share

You can try this tutorial if you want to create a dynamic health bar that also changes color according to the amount of health left. This is all done with the new UI from UNITY 4.6: Health bar turorial

6 Replies

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

Answer by duck · May 13, 2010 at 03:14 PM

Have a look at this answer, which gives source code for display health bars using the new GUI system.

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 Nallac · May 14, 2010 at 04:25 AM 0
Share

works great thanks I had to change it a bit to make it for health and get rid of the box around it but otherwise it works perfectly!

avatar image cidmodder · Jun 16, 2011 at 10:49 PM 0
Share

the link doesn't seem to work anymore. can you make a new one? Thanks!

avatar image SisterKy · Aug 07, 2011 at 09:10 PM 2
Share

duck's link probably went to this question: http://answers.unity3d.com/questions/11892/how-would-you-make-an-energy-bar-loading-progress.html

avatar image duck ♦♦ · Jun 04, 2014 at 09:21 AM 0
Share

fixed the link!

avatar image GluedBrain · Sep 08, 2014 at 06:06 AM 0
Share

If you would like to use the new Unity UI system to create a Health bar. Go to this link

Health Bar Unity 4.6

Show more comments
avatar image
1

Answer by spinaljack · May 13, 2010 at 02:10 PM

There's plenty of tutorials with health bars in them. Take a look at the 3D platformer tutorial, it has a round health bar but I'm pretty sure you can work out how to make a straight one. Also take a look at UnityGUI guide.

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
1

Answer by RyanZimmerman87 · Feb 08, 2013 at 10:37 AM

Here is a way to set this up for the player's health in case anyone is looking for that solution in the future (in C#).

This will work perfectly if you have a script attached to your main character (in this example PlayerMoveScript) with two public static variables for the player's current health (playerHealth), and their maximum health (playerHealthTotal). Just make sure that their maximum health is a float or the division will not work properly.

You can create two textures in Photoshop and size them to be identical to the Vector 2 (size) variable. Once you import the textures into Unity (.PSD extension works fine) you can switch the texture type from Texture to GUI in the Inspector.

Hope this helps someone!

     using UnityEngine;
     using System.Collections;
      
     public class PlayerHealthBarScript : MonoBehaviour
     {
      
     public GUIStyle progress_empty;
     public GUIStyle progress_full;
      
     //current progress
     public float barDisplay;
      
     Vector2 pos = new Vector2(10,50);
     Vector2 size = new Vector2(250,50);
      
     public Texture2D emptyTex;
     public Texture2D fullTex;
      
     void OnGUI()
     {
     //draw the background:
     GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y), emptyTex, progress_empty);
      
     GUI.Box(new Rect(pos.x, pos.y, size.x, size.y), fullTex, progress_full);
      
     //draw the filled-in part:
     GUI.BeginGroup(new Rect(0, 0, size.x * barDisplay, size.y));

     GUI.Box(new Rect(0, 0, size.x, size.y), fullTex, progress_full);
      
     GUI.EndGroup();
     GUI.EndGroup();
     }
      
     void Update()
     {
      
     //the player's health
     barDisplay = PlayerMoveScript.playerHealth/PlayerMoveScript.playerHealthTotal;
     }
      
     }
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
1

Answer by awplays49 · Jan 23, 2015 at 01:24 PM

I know this has been answered a ton of times, but I thought I'd answer it :)

My way of doing this:

  1. Make a UI slider and leave the canvas properties the same. Turn off interactable in the properties of the slider.

  2. Make a health variable in a script attached to the player.

  3. Make a public GameObject variable in a script attached to the healthbar. Drag the player in.

  4. Write

    using UnityEngine.UI;

After

 using UnityEngine;

  1. Make a public Slider and do

    SliderVar = GetComponent ();

SliderVar being the slider type variable.

  1. Write this code in update:

    SliderVar.value = PlayerVar.GetComponent ().HealthVar;

PlayerVar being the public GameObject you wrote in the health script, PlayerScript being the player's script, and HealthVar being the health variable in the player's script.

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 RASKALOF · Jul 30, 2014 at 05:13 AM

And you know, you can use horizontal slider its just 1 line of code

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
  • 1
  • 2
  • ›

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

12 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How do i set health to regenerate over a specified range of time? 1 Answer

Apply Damage To Player On Collison With Specific Game Object 1 Answer

Need help with this Rotation Script (fixes) 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