using System.Collections;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public static int CountEnemyAlive = 0;
public Wave[] waves;
public Transform START;
public float waveRate = 0.2f;
private void Start()
{
StartCoroutine(SpawnEnemy());
}
IEnumerator SpawnEnemy()
{
foreach (Wave wave in waves)
{
for (int i = 0; i < wave.count; i++)
{
GameObject.Instantiate(wave.enemyPrefab, START.position, Quaternion.identity);
CountEnemyAlive++;
if (i != wave.count - 1)
yield return new WaitForSeconds(wave.rate);
}
while(CountEnemyAlive>0)
{
yield return 0;
}
yield return new WaitForSeconds(waveRate);
}
}
}
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 10.0f;
private Transform[] positions;
private int index = 0;
void Start()
{
positions = Waypoints.positions;
}
void Update()
{
Move();
}
private void Move()
{
if (index > positions.Length - 1) return;
transform.Translate((positions[index].position - transform.position).normalized * speed * Time.deltaTime);
if (Vector3.Distance(positions[index].position, transform.position) < 0.2f)
{
index++;
}
if(index > positions.Length - 1)
{
ReachDestination();
}
}
private void ReachDestination()
{
GameObject.Destroy(this.gameObject);
}
private void OnDestroy()
{
EnemySpawner.CountEnemyAlive--;
}
}