- Home /
Keeping the health bar at the same length
I'm trying to create a health and mana system where if the health is 40 and later on 2000 the health bar being displayed will never change it's shape (unless health is being lost of course).
How can I do this?
Answer by flamy · Apr 29, 2012 at 07:13 PM
this is simple let us say the length of health bar is 250. you should set the length straight way, you should calculate the percent of total hp the player has rite now and multiply it with the length (250 in this case) and set it.
for example:
var maxHP:float=2000;
var currentHP:float=1750;
var percentOfHP:float;
void Update()
{
percentOfHP=currentHP/maxHP;
}
void OnGUI()
{
ExpBarLength=percentOfHP*250; /* 250 is the maximum length of the exp bar.
// set the length for exp bar.
}
PS : this method will work regardless of whether you exp bar is in 3d or GUI.
good thing it helped :)
and for more info, you can use the same logic for making a loading bar.
Answer by fafase · Apr 29, 2012 at 07:18 PM
If you use Texture2D, well, just make the pictures the same length. No big deal.
If you use the GUI, you should be using something like health/total length to display the percentage of health. Just change total length.
Your answer
Follow this Question
Related Questions
Health Bar Colour 1 Answer
Health Bar and PlyGame 0 Answers
Health bar update 0 Answers
Show GUI only when looking at an object? 1 Answer
Multiple Health Bar 1 Answer