- Home /
Enemies stop following after a few rounds
Hi All, I have built a survival spawning game whereby the enemies will spawn and move towards the player when all of the enemies are dead, the next wave spawns and so on... However, after testing and playing a few rounds, when the enemies spawn they just walk around in a circle aimlessly? I cant work out if they are losing its follow path, there is no error in the console. I will attach the script I think could cause an issue maybe.
Enemy Movement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyMovement : MonoBehaviour {
Transform player; // Reference to the player's position.
PlayerHealth playerHealth; // Reference to the player's health.- usedlater
EnemyHealth enemyHealth; // Reference to this enemy's health.
NavMeshAgent nav; // Reference to the nav mesh agent.
void Awake ()
{
// Set up the references.
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
// If the enemy has health left...
if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
// ... set the destination of the nav mesh agent to the player.
nav.SetDestination (player.position);
}
// Otherwise...
else
{
// ... disable the nav mesh agent.
nav.enabled = false;
}
}
}
Enemy Attack
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour {
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
Animator anim;
GameObject player;
// refrence to player health script so enemy can damage player
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
bool playerInRange;
// keeps everything in sync, make sure enemy isnt attack to fast or too slow
float timer;
void Awake ()
{
// locate player and store reference locally
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
anim = GetComponent<Animator>();
}
// when enemy collider is triggered
void OnTriggerEnter (Collider other)
{
// check if we have collided with player
if (other.gameObject == player)
{
Debug.Log("Player is in Range");
playerInRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
playerInRange = false;
}
}
void Update ()
{
timer += Time.deltaTime;
if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0 )
{
Debug.Log("Attacked Player");
Attack ();
timer = 0f;
}
// If the player has zero or less health...
if (playerHealth.currentHealth <= 0)
{
// ... tell the animator the player is dead.
anim.SetTrigger("PlayerDead");
Debug.Log("player dead");
}
}
void Attack ()
{
Debug.Log("ouch");
// if player is alive take some health away
if(playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage(attackDamage);
}
}
}
Player Health
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class PlayerHealth : MonoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public Slider healthSlider;
public AudioClip deathClip;
public Image damageImage;
public float flashSpeed = 5f;
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
Animator anim;
AudioSource playerAudio;
PlayerMovement playerMovement;
PlayerShooting playerShooting;
bool isDead;
bool damaged;
void Awake()
{
anim = GetComponent<Animator>();
playerAudio = GetComponent<AudioSource>();
playerMovement = GetComponent<PlayerMovement>();
playerShooting = GetComponentInChildren<PlayerShooting>();
currentHealth = startingHealth;
}
void Update()
{
if (damaged)
{
damageImage.color = flashColour;
}
else
{
damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damaged = false;
}
public void TakeDamage(int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
playerAudio.Play();
if (currentHealth <= 0 && !isDead)
{
Death();
}
}
void Death()
{
isDead = true;
playerShooting.DisableEffects();
anim.SetTrigger("Die");
playerAudio.clip = deathClip;
playerAudio.Play();
playerMovement.enabled = false;
playerShooting.enabled = false;
}
public void addHealth(int HealthAmount) {
currentHealth += HealthAmount;
if (currentHealth > startingHealth)
currentHealth = startingHealth;
healthSlider.value = currentHealth;
}
}
I thought IT may have been my health power up not resetting the health value but after debugging this is not an issue. Any guidance would be great!
I am really not sure but, you say that they stop after a few rounds? Assu$$anonymous$$g it works for couple of rounds, then I will ask you what do you do with the dead enemies? They are not blocking the path right?