When shot, enemy object disappears.. But the damage is continuously going until the player dies. Here are the scripts used on the enemy.
void FixedUpdate () {
transform.LookAt (player.transform);
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out shot)) {
targetDistance = shot.distance;
if (targetDistance < allowedRange) {
enemySpeed = 0.6f;
if (attackTrigger == 0) {
enemy.GetComponent ().Play ("Walking");
GetComponent().AddForce(transform.forward * enemySpeed * Time.deltaTime);
}
} else {
enemySpeed = 0;
enemy.GetComponent ().Play ("Idle");
}
}
if (attackTrigger == 1) {
if (isAttacking == 0) {
StartCoroutine(EnemyDamage());
}
enemySpeed = 0;
enemy.GetComponent().Play("Attacking");
}
}
void OnTriggerEnter() {
attackTrigger = 1;
}
void OnTriggerExit() {
attackTrigger = 0;
}
IEnumerator EnemyDamage() {
isAttacking = 1;
painSound = Random.Range (1, 4);
yield return new WaitForSeconds (0.9f);
screenFlash.SetActive (true);
GlobalHealth.playerHealth -= 10;
if (painSound == 1) {
hurt01.Play ();
}
if (painSound == 2) {
hurt02.Play ();
}
if (painSound == 3) {
hurt03.Play ();
}
yield return new WaitForSeconds (0.05f);
screenFlash.SetActive (false);
yield return new WaitForSeconds (1);
isAttacking = 0;
}
Second one which is responsible for enemy death.
void Update () {
if (enemyHealth <= 0) {
this.GetComponent().enabled = false;
zombie.GetComponent().Play("Dying");
StartCoroutine(EndZombie());
}
}
void DeductPoints(int damageAmount) {
enemyHealth -= damageAmount;
}
IEnumerator EndZombie() {
yield return new WaitForSeconds(3);
Destroy(gameObject);
}
↧