Seeking help with multiple enemies in a 2D Platformer

Updated on March 31, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on March 31, 2018

Hello Brackeys Forum!

 

I’m looking for a solution for this code. This is a somewhat modified version of the WaveSpawner script used in the 2d asset pack. My goal is to spawn multiple enemies with different counts, for example in the first wave I want sometimes to be spawned one type of “EnemyNumberOne” and two types of “EnemyNumberTwo” and in that same wave I want sometimes to be spawned one type of “EnemyNumberTwo” and two types of “EnemyNumberOne“. As for now this code only spawns “EnemyNumberOne” or  “EnemyNumberTwo” (depends on what number you input in “SpawnEnemy(_wave.enemy[1]);” ). If someone has a solution and helps me with this code I will be very grateful!

 
using UnityEngine;
using System.Collections;
 
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 Wave[] waves;
 private int nextWave = 0;
 public int NextWave
 {
 get { return nextWave + 1; }
 }
 
public Transform[] spawnPoints;
 
public float timeBetweenWaves = 5f;
 private float waveCountdown;
 public float WaveCountdown
 {
 get { return waveCountdown; }
 }
 
private float searchCountdown = 1f;
 
 private SpawnState state = SpawnState.COUNTING;
 public SpawnState State
 {
 get { return state; }
 }
 
void Start()
 {
 if (spawnPoints.Length == 0)
 {
 Debug.LogError("No spawn points referenced.");
 }
 
waveCountdown = timeBetweenWaves;
 }
 
void Update()
 {
 if (state == SpawnState.WAITING)
 {
 if (!EnemyIsAlive())
 {
 WaveCompleted();
 }
 else
 {
 return;
 }
 }
 
if (waveCountdown <= 0)
 {
 if (state != SpawnState.SPAWNING)
 {
 StartCoroutine( SpawnWave ( waves[nextWave] ) );
 }
 }
 else
 {
 waveCountdown -= Time.deltaTime;
 }
 }
 
void WaveCompleted()
 {
 Debug.Log("Wave Completed!");
 
state = SpawnState.COUNTING;
 waveCountdown = timeBetweenWaves;
 
if (nextWave + 1 > waves.Length - 1)
 {
 nextWave = 0;
 Debug.Log("ALL WAVES COMPLETE! Looping...");
 }
 else
 {
 nextWave++;
 }
 }
 
bool EnemyIsAlive()
 {
 searchCountdown -= Time.deltaTime;
 if (searchCountdown <= 0f)
 {
 searchCountdown = 1f;
 if (GameObject.FindGameObjectWithTag("Enemy") == null)
 {
 return false;
 }
 }
 return true;
 }
 
IEnumerator SpawnWave(Wave _wave)
 {
 Debug.Log("Spawning Wave: " + _wave.name);
 state = SpawnState.SPAWNING;
 
for (int i = 0; i < _wave.count; i++)
 {
 SpawnEnemy(_wave.enemy[1]);
 yield return new WaitForSeconds( 1f/_wave.rate );
 }
 
state = SpawnState.WAITING;
 
yield break;
 }
 
void SpawnEnemy(Transform _enemy)
 {
 Debug.Log("Spawning Enemy: " + _enemy.name);
 
Transform _sp = spawnPoints[ Random.Range (0, spawnPoints.Length) ];
 Instantiate(_enemy, _sp.position, _sp.rotation);
 }
 
}
 
  • Liked by
Reply
0 on March 31, 2018

You could pick a random number between 1 and 2 and use that number to pick which enemy you spawn. If you want to spawn patterns or have more control over what enemy is spawned when, you would need to totally re-work the spawn function.

  • Liked by
Reply
Cancel