- Home /
How to make a health bar?
Hello I am trying to make a health bar. I have got the health system in place and it decreases when the car crashes but I cant figure out how to make a health bar that goes down with the health. So for example if the health is 45 then the health bar should be 45%. I know that there is a number of health bar answers out there but for some reason none of them have worked in my game. Thank you very much in advance.
Answer by Pitomator · Dec 28, 2013 at 04:29 PM
Good question, there are many ways to do it but I like this one most:
Create a GUI Texture with your image of choice. Lets say it is 250px wide and 40px high. (for simplicity) I hope it help :) Revised Edition:
//Add your GUITexture here
var lifeBar : GUITexture;
//What ever your life is
public var life : int = 100;
//What ever you want
private var maxWidth : int = 250;
private var pixelToLiveRatio : float;
function Start(){
pixelToLiveRatio = maxWidth/life;
}
function Update(){
//Change the pixel inset.
lifeBar.guiTexture.pixelInset.width = (life * pixelToLiveRatio);
}
For some reason when I apply this script my GUITexture doesn't get smaller with the health. In fact it gets bigger upon the first loss of health and then doesn't change. This is for the Javascript code.
Sorry, I missed something. The new script should work. :)
Works perfectly but when it reaches 0 it starts increasing again. Still though thank you very much it works perfectly for what I need. Thank you very much
Your answer
Follow this Question
Related Questions
HealthBar goes negative? 1 Answer
Health Bar going 0 right away 1 Answer
Regain health on GUI 2 Answers
Pause Script 1 Answer
how to remove the health bar without destroying the script 1 Answer