Enemy just keep repeating on path and not destroying after going through last path.
waveconfig
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] List waveConfigs;
int startingWave = 0;
// Use this for initialization
void Start()
{
var currentWave = waveConfigs[startingWave];
StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}
private IEnumerator SpawnAllEnemiesInWave(waveconfig waveconfig)
{
for (int enemyCount = 0; enemyCount < waveconfig.GetNumberOfEnemies(); enemyCount++)
{
var newEnemy = Instantiate(
waveconfig.GetEnemyPrefab(),
waveconfig.GetWayPoints()[0].transform.position,
Quaternion.identity);
newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveconfig);
yield return new WaitForSeconds(waveconfig.GetTimeBetweenSpawn());
}
}
}
Enemypathing
using System.Collections.Generic;
using UnityEngine;
public class EnemyPathing : MonoBehaviour
{
private waveconfig waveconfigobj;
private List waypoints;
private int waypointIndex = 0;
void Start()
{
if (waveconfigobj != null)
{
waypoints = waveconfigobj.GetWayPoints();
if (waypoints.Count > 0)
transform.position = waypoints[waypointIndex].position;
else
Debug.LogError("No waypoints found in waveconfigobj!");
}
else
{
Debug.LogError("waveconfigobj is not assigned!");
}
}
void Update()
{
if (waypointIndex < waypoints.Count)
{
var targetPosition = waypoints[waypointIndex].position;
var movementThisFrame = waveconfigobj.GetMoveSpeed() * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementThisFrame);
if (transform.position == targetPosition)
{
waypointIndex++;
if (waypointIndex >= waypoints.Count)
{
waypointIndex = 0;
}
}
}
else
{
Destroy(gameObject);
}
}
public void SetWaveConfig(waveconfig waveconfig)
{
if (waveconfig != null)
{
this.waveconfigobj = waveconfig;
}
else
{
Debug.LogError("Trying to set a null waveconfig!");
}
}
}