- Home /
Enemy Damage problem
Hello
Im having trouble with my script I want it so when my enemys health = 0 it dies and Instantiate an exp object and the enemy dies with an explosion but when it hits the player it explodes takes life off the player but doesnt give exp heres my script
var maxHealth = 100; var curHealth = 100; var Playerdamage = 30; 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 == "Bullet"){ curHealth -= Playerdamage; }
if(collision.gameObject.Find("Player")){
target.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-Enemydamage);
var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
Destroy(gameObject);
}
}
this works for when the enemy hits the player but when i shoot the enemy and it dies i loose health and there no exp. heres my other script
var maxHealth = 100; var curHealth = 100; var Playerdamage = 30; 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 == "Bullet"){ curHealth -= Playerdamage; }
if(collision.gameObject.tag == ("Player")){
target.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-Enemydamage);
var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
Destroy(gameObject);
}
}
this works for when i shoot the enemy it dies i DONT loose health and the exp object appears. but all im changing is
if(collision.gameObject.Find("Player"))
to
if(collision.gameObject.tag == ("Player"))
im really confused can someone please help
Thanks.
Answer by Owen-Reynolds · Apr 19, 2011 at 03:33 PM
if(collision.gameObject.tag == "Bullet")
checks the TAG (from the tag dropDown) of whatever you hit. Maybe its actual name is blasterBolt or creamPie, but it has a Bullet tag.
Find is for looking for other gameObjects in the Inspector, by name. For example, in Start you could put target=GameObject.Find("Player"));
. It does the same as dragging the Player into target in the Inspector.
if(collision.gameObject.Find("Player"))
isn't completely legal. It probably gets turned into big-GameObject Find. So, whenever the enemy hits anything, it checks whether a player exists anywhere in the game, and hurts him.