So Im making a medieval Zombie game, I finished my Enemy AI, it follows the player, it has a character Controller so it wont go through walls or anything, but whenever I kill ONE zombie, ALL the other zombies die too. I think the problem is that the character controller is being destroyed but Im not sure. I need the Controller so the Zombie wont go through objects. Here is my code
enter code here
var speed : float = 5; // the speed
var controller:CharacterController;
static var enemyLife : int = 100; //health
var Explosion : GameObject; //Explosion When Enemy is hit
var Death : GameObject; //explosion when enemy dies
function Start()
{
controller = gameObject.GetComponent(CharacterController);
target = GameObject.FindWithTag("Player").transform; //targets the player
}
function Update()
{
transform.LookAt(target); // the NPC looks at the player
// Here you access the Character Controller component and move your NPC with SimpleMove() giving the speed and the direction(forward).
//As the NPC is looking at you, forward is in your direction.
controller.SimpleMove(speed*transform.forward); // Moves forward
if (enemyLife < 1) //Death
{
Destroy(gameObject); //Destroys enemy
var instance8 = Instantiate(Death,transform.position,transform.rotation); //Explodes
Debug.Log("Enemy was killed");
}
}
//THIS FUNCTION IS FOR WHEN ATTACKS HIT ZOMBIE
function OnCollisionEnter (other : Collision)
{
if(other.gameObject.name == "FireBall(Clone)") //If FireBall hits it
{
enemyLife -=18;
if(Amulet.EAmulet == true) //If Player has a fire amulet
{
Debug.Log("EAmulet is BALLAH");
enemyLife -= 2;
}
Destroy(other.gameObject);
var instance = Instantiate(Explosion,transform.position,transform.rotation);
Debug.Log("Enemy was hit");
}
else if(other.gameObject.name == "PhotonBall(Clone)") //If Photon Ball hits
{
enemyLife -= 20;
if(Amulet.LAmulet == true) //if player has a light amulet
{
Debug.Log("Like a Diamond");
enemyLife -= 1;
}
Destroy(other.gameObject);
var instance2 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name =="Shock(Clone)")
{
enemyLife -= 23;
Destroy(other.gameObject);
var instance3 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "FrostBall(Clone)")
{
enemyLife -= 25;
Destroy(other.gameObject);
var instance4 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "AquaBall(Clone)")
{
enemyLife -= 34;
Destroy(other.gameObject);
var instance5 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "PlasmaBall(Clone)")
{
enemyLife -= 35;
Destroy(other.gameObject);
var instance6 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "SpitFire(Clone)")
{
enemyLife -= 16;
if(Amulet.FAmulet == true)
{
Debug.Log("FAmulet Works");
enemyLife -= 2;
}
Destroy(other.gameObject);
var instance7 = Instantiate(Explosion,transform.position,transform.rotation);
}
}