Adding Enemy Spawn Waves to 2D Galaxy Shooter in Unity-Part 02!

Objective: Part 02 of implementing a 5 wave spawn system that spawns an increasing number of enemies with each wave.

Creating a Modular Coroutine for Enemy & Powerup Wave Spawning

Let’s assign the _remainingEnemies to the parameter so the UI can track the remaining enemies. Assign _co the SpawnCommonPowerupsRoutine() so we can stop this further down with a StopCoroutine() call.

Now we jump into a while loop, and every 5 seconds a new enemy is going to spawn via the SpawnRoutine() method.

Within this while loop let’s have an If check for whether the _remainingEnemies ever go below 1 and if they do we are going to assign the variable to 0 so it doesn’t go negative. Then we are going to stop the SpawnCommonPowerupsRoutine() via a StopCoroutine() call. Next, let’s increase the _currentWave number by 1 and call the NextWave() method which will then run the same coroutine but as ‘Wave 2’.

SpawnRoutine()

Let’s create some local variables _randomX, _randomY and _randomEnemy to bring randomness to our enemy spawns and enemy types. _randomX and _RandomY are going to be used for the X and Y axis respectively.

The Vector3 variables are going to be used for the spawning locations of the 3 enemies.

Then we dive into the switch statement which compares the expression _randomEnemy number. It will then instantiate the associated enemy to that case number.

Once an enemy has been instantiated, it breaks out of the switch statement and we deduct 1 from _remainingEnemies.

WaveText() Modular Coroutine Within “UIManager” Script

This modular coroutine simply takes one parameter (the current wave number) and is input into a concatenated string. The game object turns on for 2 seconds before being turned off again.

Voila!

The next article is going to look at incorporating common and rare powerup spawn routines into this existing modular coroutine.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store