- Home /
Enemy healthbar script
Hello Unity Community, i've been lookin all over the web for an enemy healthbar that are not a GUI Texture or is only shown near the enemy.
the Healthbar should be above the enemy head within the game or placed on the screen as a GUI when near the enemy
Any suggestions on how? or where i might find a tutorial?
Heres the Enemy Health Javascript:
#pragma strict
var Health = 100;
var Enemy : GameObject;
var Die : AudioClip;
//var DieAnimation : AnimationClip;
function Update ()
{
if(Health <= 0)
{
Dead();
}
}
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
}
function Dead()
{
//animation.Play("DieAnimation");
audio.PlayOneShot(Die);
Destroy (gameObject, 1);
}
Thanks in advance
Answer by Skalde · May 07, 2013 at 04:46 PM
I found an even more simple solution by using a plane like this:
#pragma strict
var HealthBar : Transform;
private var MaxHealth : float = 100;
private var MaxBar : float = 0.2;
var Health = 100;
var Enemy : GameObject;
var Die : AudioClip;
var EnemyDoll : Transform;
//var DieAnimation : AnimationClip;
function Update()
{
// --- Change Size Of Bar --- \\
transform.localScale.z = (MaxBar) * (Health/MaxHealth);
// --- Change Colour Of Bar --- \\
if(transform.localScale.z == 0.5)
{
transform.renderer.material.color = Color.green;
}
if(transform.localScale.z <= 0.35)
{
transform.renderer.material.color = Color.yellow;
}
if(transform.localScale.z <= 0.1)
{
transform.renderer.material.color = Color.red;
}
if(Health <= 0)
{
Dead();
}
}
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
}
function Dead()
{
//animation.Play("DieAnimation");
audio.PlayOneShot(Die);
EnemyDoll.animation.Play("Enemy die");
Destroy (Enemy, 1);
}
But thanks anyways ^^
Answer by SubatomicHero · May 07, 2013 at 12:20 PM
Would it make sense to make the GUI Bar you have as a child of the enemy in the hierarchy pane?
Position it where you want, then wherever the enemy moves, the bar will follow.
Its not possible to get the GUI to follow the enemy just by parentin it.. thats why i need a good example of a script that makes it possible, but thanks for your answer :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can an enemy deplete players health? 0 Answers
Enemy Health Bar 1 Answer