- Home /
Damage taking?
This is my code for a health bar script, yet I am unsure of how to make my health/health bar size decrease accordingly;
@script ExecuteInEditMode()
var healthTexture : Texture2D;
var healthBorder : Texture2D;
var health : int = 100;
function OnGUI () {
GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
var adjust : int = health * 3; //adjusting texture size (width) / health(100) GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15)); GUI.DrawTexture(Rect(0,0,290,15), healthTexture); GUI.EndGroup(); }
it is easier if you use 2 variables for health, like maxHP (maximum health points) and curHP (current health points). when you divide maxHP by curHP you get a percentage that you can use to multiply the width of your rectangle with.
Answer by Myhijim · Aug 13, 2012 at 11:10 PM
In the awake you set
var health = 100;
var currHealth = 100;
var maxHealth = 100;
In the update you set the health
health = currHealth/maxHealth
In the on GUI
Put in your code BUT replace
GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
And
var adjust : int = health * 3
With
GUI.DrawTexture(Rect(0,0,health,15), healthTexture);
And var adjust : int = currHealth * 3
Im pretty sure that will work ~Myhijim
When I put the updated code in it comes up with errors and tells me do this;
@script ExecuteInEdit$$anonymous$$ode()
function Awake () {
var health = 100;
var currHealth = 100;
var maxHealth = 100;
health = currHealth/maxHealth;
var healthTexture : Texture2D;
var healthBorder : Texture2D;
// At first we draw border/background texture (with current texture it isn't important) GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
//Now on top we can put Health Bar texture var adjust : int=currHealth*3; //adjusting texture size (width) / health(100) GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15)); GUI.DrawTexture(Rect(0,0,health,15), healthTexture); GUI.EndGroup(); }
How would I also apply damage?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Health and damage reciever 1 Answer
Problem with enemy health. 1 Answer
Damage script 0 Answers
this is part of my health script it gives me a error unknown identifier GameScore pls help thxxx 0 Answers