- Home /
How to create an NPC enemy which can take damage and be killed when its energy or health reaches zero?
Hi, I am a student who wants to create a 'RPG' style 1 level game for my Final Major Project at college. We have no teachers in the college with knowledge of Unity and its the first time using it myself, I have managed to teach myself how to attach animation to keys (for walking etc) and create a controllable 3rd person character.
However I would like to know how to create a killable NPC enemy character?
Any help would be great, thanks!
Answer by Not showing my name · Apr 21, 2010 at 12:07 PM
Go to
and then click to support, then resources, and then fps tutorial. download the completed thing and then get "Damage Receiver [Script]"
here's the link DIRECTLY TO THE DOWNLOAD.
Awesome question!
Plus, you can make a ragdoll of the character so when it's destroyed it collapses.
Thank you! this tutorial looks like it will be a great help in more than just this aspect of the game, going to get stuck into it. Thanks again.
you're welcome. I made a game called "High Security jail cell remake and you have 2 guns: a machine gun and a rocket launcher.
Answer by duck · Apr 21, 2010 at 09:40 AM
At a very basic level, you'd need to have a script on your NPC which has a variable containing its current amount of health, and a function which is designed to be called by other weapons or hazards, which reduces this health value.
This function would then be responsible for reducing the health value, and killing and removing the NPC when its health reaches zero.
For example:
var health = 100.0;
function ApplyDamage( amount : float ) {
health -= amount;
// <- (update on-screen health displays here)
if (health < 0) {
Die();
} else {
// <- (play "hurt" animation & sound here)
}
}
function Die() {
// <- (play "die" animation & sound)
yield WaitForSeconds( dieDuration );
// now remove this gameObject from the scene:
Destroy(gameObject);
}
Thanks you for the quick and helpful reply, much appreciated! I will give this a shot while at college and see how i get on with it, thanks again.
Your answer
Follow this Question
Related Questions
Health below zero but shows negative numbers 1 Answer
Object Not Recieving Damage 3 Answers
One enemy triggers all the enemies 2 Answers
Problem with enemy health. 1 Answer
The enemy don´t lose health, but why??? 0 Answers