a Script for Enemy HP

ive been searching through tutorials, forums, and answers, and haven’t found any good scripts.

the best one i found so far is this script,

function ApplyDamage (damage : float) {
    // We already have less than 0 hitpoints, maybe we got killed already?
var maxHealth = 100;
var curHealth = 100;
var Playerdamage1 = 8;
var Playerdamage2 = 10;
var Playerdamage3 = 15;
var Playerdamage4 = 100;
var Enemydamage = 10;
var target : GameObject;
var explosion : GameObject;
var exp : GameObject;
 
function Update(); {
    AddjustCurrentHealthEnemy(0);
}
 
function AddjustCurrentHealthEnemy(adjE) {
    curHealth += adjE;
 
    if(curHealth < 0)
        curHealth = 0;
 
    if (curHealth > maxHealth)
        curHealth = maxHealth;
 
    if(maxHealth < 1)
        maxHealth = 1;
 
    if(curHealth < 1) { //die
        var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
        var newExp:GameObject=Instantiate(exp,transform.position,transform.rotation);
        Destroy(gameObject);
    }
}
 
function OnCollisionEnter(collision : Collision){
    if(collision.gameObject.tag == "MG"){
        curHealth -= Playerdamage1;
    }
    
function OnCollisionEnter(collision : Collision){
    if(collision.gameObject.tag == "PISTOL"){
        curHealth -= Playerdamage2;
    }
    
function OnCollisionEnter(collision : Collision){
    if(collision.gameObject.tag == "SHOT"){
        curHealth -= Playerdamage3;
    }
    
function OnCollisionEnter(collision : Collision){
    if(collision.gameObject.tag == "ROCKET"){
        curHealth -= Playerdamage4;
    }
    

 
    if(collision.gameObject.Find("Player")){
        target.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-Enemydamage);
        var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
        Destroy(gameObject);
    }
}

but it doesnt work quite right. it asks at line 15 to replace the ‘Update’ with a (

and at line 16, it wants me to replace the ; with a :

but these break the script. whats wrong with it? also, if you know, i have many classes of bullets in the game. at lines 38-56, have the different bullet classes. if i did those right please inform me.

This script is kind of a mess. Here is it cleaned up and optimised:

//Got rid of ApplyDamage() here, because it seemed to bear no purpose

var maxHealth : int = 100; //Typed these, as dynamic typing could cost you precious execution speed
var curHealth : int = 100;
var playerDamage1 : int = 8; //Changed these to camelCase
var playerDamage2 : int = 10;
var playerDamage3 : int = 15;
var playerDamage4 : int = 100;
var enemyDamage : int = 10;
var target : GameObject;
var explosion : GameObject;
var exp : GameObject;
 
function Update() { //Got rid of ; here
    AddjustCurrentHealthEnemy(0);
}
 
function AddjustCurrentHealthEnemy(adjE) {
    curHealth += adjE; //I have no idea what adjE is, but I will leave it in there just in case (seems to be doing nothing)
 
    curHealth = Mathf.Clamp(curHealth, 0, maxHealth); //Simplified
    maxHealth = Mathf.Max(1, maxHealth);
 
    if(curHealth < 1) { //die
        var newExplosion : GameObject = Instantiate(explosion, transform.position, transform.rotation);
        var newExp: GameObject = Instantiate(exp, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}
 
function OnCollisionEnter(collision : Collision) { //Don't need separate OnCollisionEnter functions
    if(collision.CompareTag("MG")){
        curHealth -= playerDamage1;
    } else if(collision.CompareTag("PISTOL")){
        curHealth -= playerDamage2;
    } else if(collision.CompareTag("SHOT")){
        curHealth -= playerDamage3;
    } else if(collision.CompareTag("ROCKET")){
        curHealth -= playerDamage4;
    } else if (collision.CompareTag("Player")) { //Make sure to tag player, "Player"
        collision.gameObject.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-enemyDamage);
        var newExplosion: GameObject = Instantiate(explosion, collision.gameObject.transform.position, collision.gameObject.transform.rotation);
        Destroy(collision.gameObject);
    }   
}

I’m not sure if it will work, as I haven’t tested. Also, there seemed to be a lot of ambiguous stuff in there, so I commented all the stuff I changed. :slight_smile:

Hope that helps you,

Klep


EDIT:

Try it now. :slight_smile: