Question by
andreyazbyn · Oct 09, 2015 at 03:15 PM ·
c#enemyenemy spawnwaves
wave spawner difficulty increase
there's the script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WaveSpawner : MonoBehaviour {
public enum SpawnState { Spawning, waiting, counting};
[System.Serializable]
public class Wave {
public string name;
public Transform enemy;
public int count;
public float rate;
}
public List<Wave> waves = new List<Wave>();
private int nextWave = 0;
public Transform[] spawnPoints;
private SpawnState state = SpawnState.counting;
public float timeBetweenWaves = 5f;
public float waveCountdown;
private float searchCountdown = 1f;
public Transform normalEnemy;
public int currentCount;
public float currentRate;
void Start() {
waveCountdown = timeBetweenWaves;
currentCount = 2;
currentRate = 3;
}
void Update() {
if (state == SpawnState.waiting) {
if (EnemyIsAlive() == false)
{
WaveCompleted();
}
else {
return;
}
}
if (waveCountdown <= 0)
{
if (state != SpawnState.Spawning)
{
StartCoroutine(SpawnWave(waves[nextWave]));
}
}
else
{
waveCountdown -= Time.deltaTime;
}
}
void WaveCompleted() {
state = SpawnState.counting;
waveCountdown = timeBetweenWaves;
//TODO add more waves
// if (nextWave + 1 > waves.Count - 1)
// {
currentCount += 2;
CreateWave(normalEnemy, currentCount, currentRate);
// Debug.Log("All Waves Complete");
//}
nextWave++;
}
private void CreateWave(Transform Enemy, int Count, float rate)
{
Wave wave = new Wave();
currentCount = wave.count;
currentRate = wave.rate;
wave.name = "autoGenerated Wave";
wave.count = Count;
wave.rate = rate;
wave.enemy = Enemy;
waves.Add(wave);
}
bool EnemyIsAlive() {
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("Enemy") == null)
{
return false;
}
}
return true;
}
IEnumerator SpawnWave(Wave _wave) {
state = SpawnState.Spawning;
// currentCount = _wave.count;
//currentRate = _wave.rate;
for (int i = 0; i < _wave.count; i++) {
SpawnEnemy(_wave.enemy);
yield return new WaitForSeconds(1 / _wave.rate);
}
state = SpawnState.waiting;
yield break;
}
void SpawnEnemy(Transform _enemy) {
Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(_enemy, _sp.position, _sp.rotation);
}
}
why doesn't it work?
Comment
Why do you think it doesnt work?
Whats broken about it?
What's the problem? The enemies don't spawn with increasing difficulty? There's no difficulty variable. You're not clear in your question. Unless you mean, difficulty is the actual number of enemies spawned. $$anonymous$$aybe has something to do with resetting currentCount everytime you spawn a wave.
Wave wave = new Wave();
currentCount = wave.count; // <-- is this intended?
currentRate = wave.rate;
Answer by andreyazbyn · Oct 10, 2015 at 06:47 AM
i found the problem these two lines
currentCount = wave.count;
currentRate = wave.rate;
after removing them everything works fine