x


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

more ▼

asked May 13, 2010 at 01:31 PM

Nallac gravatar image

Nallac
13 1 3 7

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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

more ▼

answered May 13, 2010 at 03:14 PM

duck gravatar image

duck ♦♦
47.3k 129 188 457

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!

May 14, 2010 at 04:25 AM Nallac

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

Jun 16, 2011 at 10:49 PM cidmodder
Aug 07, 2011 at 09:10 PM SisterKy

fixed the link!

Jun 04 at 09:21 AM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 13, 2010 at 02:10 PM

spinaljack gravatar image

spinaljack
9.8k 26 38 108

(comments are locked)
10|3000 characters needed characters left

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;
    }

    }
more ▼

answered Feb 08, 2013 at 10:37 AM

RyanZimmerman87 gravatar image

RyanZimmerman87
2.2k 49 941 59

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 30 at 05:13 AM

Deatrock gravatar image

Deatrock
0

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x8756
x5983
x4755
x589
x157

asked: May 13, 2010 at 01:31 PM

Seen: 48638 times

Last Updated: Jul 30 at 05:13 AM