- Home /
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.
Answer by Kleptomaniac · Mar 10, 2013 at 11:51 PM
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. :)
Hope that helps you,
Klep
EDIT:
Try it now. :)
i really appreciate the help, and im sorry for complaining, but i think it broke it. :I
it made many of the values unknown, i fixed most of them but what i don't know is how to fix the collision sensor
Oh, whoops, yeah, just noticed some other errors and bits where I forgot to change the variable na$$anonymous$$g. Hang on I'll edit.
still doesn't work ;_; here are the errors it keeps co$$anonymous$$g up with,
Assets/SpecialScripts/EnemyHP.js(15,8): BCE0005: $$anonymous$$ identifier: 'collision'.
Assets/SpecialScripts/EnemyHP.js(16,9): BCE0005: $$anonymous$$ identifier: 'collision'.
Assets/SpecialScripts/EnemyHP.js(16,43): BCE0005: $$anonymous$$ identifier: 'PlayerHealth'. (i know how to fix this)
Assets/SpecialScripts/EnemyHP.js(17,51): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
Assets/SpecialScripts/EnemyHP.js(18,17): BCE0005: $$anonymous$$ identifier: 'collision'.
Assets/SpecialScripts/EnemyHP.js(27,28): BCE0023: No appropriate version of 'UnityEngine.$$anonymous$$athf.Clamp' for the argument list '(int, int)' was found.
Assets/SpecialScripts/EnemyHP.js(31,52): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
Assets/SpecialScripts/EnemyHP.js(32,45): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
O$$anonymous$$, that should pretty much work now. Updated post again.
It's made harder by the fact that I don't have Unity on this computer, so I'm having to debug in my brain. :D
everything's fixed :D thanks very much, id uprate you or something if i could but im not allowed to :I
Your answer

Follow this Question
Related Questions
Ai that applies damage in collision? 1 Answer
Health Tutorial broken link to script 4 Answers
How can an enemy deplete players health? 0 Answers
AI keeps attacking objects that are not player. 1 Answer
C# Cooldown not working 3 Answers