- Home /
I have an attack/take damage script how do I make the player die when health hits 0?
So as my question asked I already have my self a Script to take damage when my AI hits me or collides with me. I don't want it to go to -10 -20 but when I hit 0 I respawn at a set location
using UnityEngine;
using System.Collections;
public class DamagePlayer : MonoBehaviour
{
public int playerHealth = 100;
int damage =10;
void Start()
{
print(playerHealth);
}
void OnCollisionEnter(Collision _collision)
{
if (_collision.gameObject.tag == "TheOneThatKillsTheLeast")
playerHealth -=damage;
print("I have taken damage" + playerHealth);
}
}
will I have to link to a new code or is it possible to take damage and kill within this script. thanks all for the help.
Comment
Best Answer
Answer by allenallenallen · Jan 31, 2017 at 05:21 PM
If you want to do it all in the same void in the same script:
void OnCollisionEnter(Collision _collision)
{
if (_collision.gameObject.tag == "TheOneThatKillsTheLeast")
playerHealth -=damage;
if (playerHealth <= 0){
playerHealth = 0;
// kill and respawn player here
}
print("I have taken damage" + playerHealth);
}
}
Hey thanks for the help it worked great now thanks for the help, sorry for the slow reply BTW been a busy week.