- Home /
Hurting and getting hurt
Hey I am making an FPS alien game. I have no clue how to get hurt when the "alien" hits me and how to make him die when I hit him. I have no script and would like one since I am a noob. Thanks
Answer by Peter G · May 13, 2010 at 12:59 AM
The tutorials do a good job explaining this. Here is the 3rd Person Platformer.
Basically here is the idea. The script below would go on the enemy
var damage : int = 5; var damageRadius = 1;
var player : Transform; //drag the player here in the inspector.
function Update () { if(Vector3.Distance(transform.position, player.position) <= 1) {
//Alien Attacks
player.BroadcastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReciever);
}
}
Create another script for the player with the following:
//This next function is a script on the player. var health = 10;
function ApplyDamage(damage : int) { health -= damage;
if(health <= 0) {
Die();
}
}
function Die () { //Respawn Player if wanted.
Destroy(gameObject);
}
Some of the"//" kinda look like they should be in the script, should tweak this or take off some of the "//" or do nothing?
Yes, they would be in a script, but I commented it out to signify that they would not be in this script. I'll edit my post to make more sense.
thank you very much for the scripts I will have to see if these work!
Okay so I just want to clarify this. THE FIRST SCRIPT GOES ON THE ALIEN WHILE THE SECOND IS ON THE DUD$$anonymous$$ I $$anonymous$$A$$anonymous$$E $$anonymous$$Y PLAYER IN THAT var Player:Transform. Now is their anything else I need to do or is it that when I add these scripts and I get close to the alien he hurts me? Also how do I have a HUD, or GUI to show my health.
You are correct in which script goes on which and how to set it up.
A HUD is really a different question. I would search for it in the Answers, and the tutorial I gave you explains it. Basically create a GUI Script that finds the player, retrieves his health variable and displays an image accordingly.
Your answer
![](https://koobas.hobune.stream/wayback/20220612232402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gun Firing help? 2 Answers
How to let my gun shoot ?? 0 Answers
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers
Question about FPS and Raycasting Javascript 2 Answers
Unity FPS question 0 Answers