- Home /
wave spawner with different enemies in the waves
There is a lot of wave spawner scripts and tutorials out there but all of them can't spawn few different enemies in one wave. Except this one from Space Shooter tutorial (was just a bit edited by me ). What I want is that I could spawn first wave with some different enemies, but second wave with another different enemies and so on. It could be cool to set for every wave special spawnValues too. My problem is that I have no slightest notion how to make it, my programming skill is just far away from this task. So could someone help me? Please:)
{
public GameObject[] hazards;
public GUI healthBar;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public int numberOfWaves;
void Start ()
{
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
//while (true)
while (numberOfWaves > 0)
{
for (int i = 0; i < hazardCount; i++) {
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
numberOfWaves--;
if (numberOfWaves == 0)
{
break;
}
}
}
}
If you are going to change the question after its been answered please notify me so I can update people who have answered so that their answers are still relevant.
If its a completely different question please create a new question because if there are alot of answers it would be really confusing for anyone trying to find similar answers.
Answer by sacredgeometry · Aug 11, 2019 at 04:09 PM
Try this
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
[Serializable]
public class WaveEnemy
{
public int Amount;
public GameObject Type;
}
[Serializable]
public class Wave
{
public List<WaveEnemy> Enemy;
}
public class Spawner : MonoBehaviour
{
public List<Wave> Waves;
public int CurrentWave = 0;
void CreateNextEnemyWave()
{
var wave = Waves.ElementAtOrDefault(CurrentWave);
if(wave != null) {
CreateEnemiesFor(wave);
CurrentWave++;
}
}
void CreateEnemiesFor(Wave wave)
{
if(!wave.Enemy.Any()) return;
foreach (var enemy in wave.Enemy)
{
for (int i = 0; i < enemy.Amount; i++)
{
Instantiate(enemy.Type, Vector3.zero, Quaternion.identity);
}
}
}
// To test press spacebar
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)) CreateNextEnemyWave();
}
}
thank you for your answers Yes I want each wave to have an exact set of enemies, so it should be 2D array then. Is there a good tutorial to use it?
this script I like more :) Thank you . I have to go sleep at the moment, so I will test the script tomorrow and write you
Answer by Bunny83 · Aug 11, 2019 at 06:18 PM
There has been countless of such questions already. Here's the one I wrote a long time ago. However it highly depends on your requirements.
thank you, I saw this but I didn't get that there is ability to spawn different enemies within one wave. Now I see it and going to try to adjust your script to my needs
Your answer
Follow this Question
Related Questions
How do I add multiple different enemies to a wave spawner? 2 Answers
Trouble with Object Pooled Enemy Waves 0 Answers
Enemy Spawn System with time and limit enemy in game 2 Answers
NEED HELP! Wave spawner just wont work!,NEED HELP! Wave spawner not working :( 2 Answers
Enemy Respawn After Death (MMO Style) C# 5 Answers