- Home /
How to make your player health subtract when enemies come into contact with you
Hi, I am developing a FPS game where enemies come chasing after you.
I am having trouble developing a system where when the enemies come into contact with me, my health decreases.
Can someone kindly help me with the scripting how to do this, and how do I setup a GUI display for my health as well.
Thank you very much.
Answer by Justin Warner · Dec 05, 2010 at 03:07 PM
Get distance using
distance = Vector3.Distance(ENEMY, PLAYER);
Check IF the distance is less than 1, than destroy the enemy chasing...
if(distance <= 1)
{
//You can add in an animation to explode/whatever else you want when it hits the player.
Destroy(gameObject)
}
ELSE, keep chasing.
else
{
Chase();
}
I'd separate the GUI question, but their are tutorials on it. Check this guy out: http://www.burgzergarcade.com/search/node/health
Answer by Richard J. Hansen · Apr 29, 2011 at 01:36 AM
In my game I have a large monster that attacks with melee attacks. During melee attacks I create a box collider object around the monster's claw (the object is a child of the claw's bone). If the collider hits the player I have it deal damage and then destroy itself to avoid doing repeat damage. The collider also has a timer to destroy itself when the attack is over.
Answer by Scott 9 · Apr 29, 2011 at 03:30 AM
OnCollisionEnter(Collision col){
if (col.tag == "enemy") {
health-= col.gameObject.GetComponent<EnemyScript>().DamageAmt;
// or maybe just health--;
}
}
you will have to add custom logic for what to do when your health reaches 0 of course.
Your answer
Follow this Question
Related Questions
Damage script is screwed up...? what to do? 1 Answer
FPSTutorial: AI robot not taking any damage! 2 Answers
Fire Damage 2.0 1 Answer
Damage not triggering in build 1 Answer
How do I create projectile weapons that all have travel time? 0 Answers