x


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.

more ▼

asked Dec 05 '10 at 01:18 PM

Cyberkuta gravatar image

Cyberkuta
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered Dec 05 '10 at 03:07 PM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

Thank you very much for your kind help.

Dec 12 '10 at 07:26 PM Cyberkuta
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 29 '11 at 01:36 AM

Richard J. Hansen gravatar image

Richard J. Hansen
171 4 4 12

(comments are locked)
10|3000 characters needed characters left
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.

more ▼

answered Apr 29 '11 at 03:30 AM

Scott 9 gravatar image

Scott 9
6 7 7 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x383
x248
x9

asked: Dec 05 '10 at 01:18 PM

Seen: 1724 times

Last Updated: Dec 05 '10 at 01:18 PM