When player get close to an enemy and the enemy starts attacking the player, all the enemies throughout the map start their attacking animation. Player's health get deducted and go to negatives. Player never dies but keeps getting hit by enemies that are not even close to him. Here are the two scripts used on enemies.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieFollow : MonoBehaviour {
public GameObject player;
public float targetDistance;
public float allowedRange = 10;
public GameObject enemy;
public float enemySpeed;
public static int attackTrigger;
public RaycastHit shot;
public int isAttacking;
public GameObject screenFlash;
public AudioSource hurt01;
public AudioSource hurt02;
public AudioSource hurt03;
public int painSound;
// Use this for initialization
void Start () {
}
// Update is called once per frame
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour {
public int enemyHealth = 10;
public GameObject zombie;
// Use this for initialization
void Start () {
}
// Update is called once per frame
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);
}
}
↧