Need a Health Bar script for my game.
I'm currently making a space shooter game in which when a collider(asteroids, enemy ship, lasers etc.) hits my spaceship the health value decreases by 1 out of 10. Can anybody help me to make this script, I'm a newbie and my knowledge in C# is not much.
Answer by Sgt_Spike · Jun 19, 2019 at 06:10 PM
Hi. Assuming you already have colliders set up for the objects, a script like this should work:
public int maxHealth = 10;
private int health;
private bool playerDied;
void Start()
{
playerDied = false;
health = maxHealth;
}
private void OnTriggerEnter(Collider obj)
{
if(col.tag == "debris")
{
DecreaseHealth();
}
}
private void DecreaseHealth()
{
health = health - 1;
if(health <= 0)
{
playerDied = true;
}
}
Haven't tried the script myself but it should work. Just set up a tag for all the objects called 'debris' or whatever you want and try it out! Hopefully it helps.
Your answer
Follow this Question
Related Questions
Need script for simple heath system 1 Answer
Third person game hit a ball 0 Answers
unity Third person uwp 0 Answers
Space shooter tutorial scrolling background: which is the better way of implementing it? 2 Answers
player is not moving 1 Answer