- Home /
Enemy Health Bar
i got this piece of simple code that works very well.
var hit = 0;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Bullet")
hit +=1;
checkhit();
if(other.gameObject.tag == "Bullet1")
hit +=5;
checkhit();
}
function checkhit(){
if(hit == 5){
Destroy(gameObject);
}
}
Is there anyone out there who can help me please make a small health bar on top of the parent of the script that displays health as the numbers progress?
Something like this...
or this....
And how would i go about implementing hit numbers that display amount of damage like -1 as i hit them?
like this
Answer by Reder13 · Jan 16, 2015 at 08:41 PM
Well first of all i think you should change your code to:
function checkhit(){ if(hit >= 5){ Destroy(gameObject); } }
that way if you skip over 5 they still get destroyed.
second to make a simple health bar use GUI.HorizontalSliders they work wonders just add a GUI skin to make it red.
to display the health text just use a GUI Text something like
public GUIText healthText;
public int maxHealth;
private int curHealth;
void OnGUI(){
healthText.text = "( " + curHealth + " / " + maxHealth + " )";
}
however im still a beginner so im not quite sure how you wanna make the bar and text follow around the characters but i hope this helps a little.
And look on YouTube for unity tutorials there are lots that take you through all the code. If you're starting coding from scratch I'd recommend C# but hey it's your choice.
Your answer
Follow this Question
Related Questions
Enemy healthbar script 2 Answers
Change Health of a duplicated enemeis 1 Answer
Health to come above enemy when clicked 1 Answer
Problem with 2 scripts communicating 0 Answers
Taking a hit 3 Answers