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!

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);
}

Go to

unity3d.com

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.

http://unity3d.com/support/resources/tutorials/fpstutorial

Awesome question!