Enemy Spawn Controller

This week I worked on making an enemy spawn controller object which allows me to implement the different levels we want to have in our game, the levels being waves of enemies varying in size and in the types of enemies that spawn. Using this object I can spawn as many of each type of enemy as I want at different locations on the screen, but for the purpose of our game I only needed to make them spawn from the right side on the screen.


The way this was setup makes it very easy to add new types of enemies to the spawn controller just by simply increasing the size of the Enemies array(of type GameObject) and dragging the new enemy prefab into the new slot, and this makes it easy to integrate newer enemy types through out any stage of production . It is also easy to change the positions where they spawn by changing the “Spawn Position Values” X, Y, and Z values as shown in the image above, which are world space coordinates and not pixel positions. For my spawn controller, I use the X component of the “Spawn Position Values” vector3 as is, but use the Y component to choose a random elevation to spawn the enemies, and the Z component is ignored because the game is in 2D, but is still needed because the Instantiate function I am using takes a vector3 parameter to determine the position where it creates an instance of the object. These values can also be changed at any time through out the game which allows us to create specific spawn patterns that can be used in the tutorial phase or when we want to introduce a new mechanic to our player.

Some other variables that can be changed easily are the wait time before the spawning starts, the wait time between each enemy spawning, and the wait time between waves. The way these delays can be added to code is by using a coroutine which is a function that can suspend its execution until a certain provided time has passed. This allows us to manipulate the spawning times and create spawning rhythms that help in invoking different feelings in the player, for example, we can make the spawning time between the enemies very fast to make the player feel stressed, or slow it down in the tutorial phase or during the first few waves to allow the player to get used to the different mechanics.

Internally this script keeps track of the wave number and the number of each type enemy to be spawned in that wave. There are some initial values for the number of  each enemy type and depending on what wave you are on these values are changed, for example, on waves 1 to 3 you start with only the one enemy type spawning but increase to number that spawns each wave, on wave 4 you introduce the second type of enemy, and increase the number of that type of enemy for the next 3 waves while decreasing it for the first type.

When the numbers for each type of enemy in the wave are decided, the corresponding index for that enemy(reminder that the enemy game objects are stored in an array as mentioned earlier) is added to a List of type integer which then has its components shuffled around to randomize the spawning order of the enemies using the a variant of the Fisher-Yates shuffling algorithm, and I do this to add replay value to the game and make it a bit unpredictable. After that the List gets iterated upon and the enemies get spawned, and also I have to mention that the List is cleared of its data after every wave.

I am generally happy with this artifact, but it can be and will be improved to make it more easy for my game designer to use without having to edit the code.


2 thoughts on “Enemy Spawn Controller

  1. The blog is well written, informative and clear. I am not a programmer yet I understand exactly what you have done and you have explained how your choices were made for furthering the initial game concept. As an artist, I felt a learned a little bit more about the programming side of game making. I like that you give examples of the impact change in enemy spawn time, duration and waves have on the aesthetics of the game, or rather what emotions these changes can invoke for the player. The blog could be more readable if there where more pictures, just so that you could get a feeling for the game and imagine how the enemy spawning would work in game. It would also be nice to see more personal reflection, whether you learned anything new from creating this spawner, and how you are thinking of improving it for the future.


  2. Hey Jad,

    First off, nice post 🙂 It’s always fun to see how people solve different problems they face while making their games. I can’t help but pretty much agree with noirjesus, the post was well written and informative.
    However, when it comes to the clearness I did have one thing that I couldn’t really wrap my head around. In the section where you talk about using the Vector3 values for determining the spawn positions you mention that the Y value is used to determine the random elevation of the enemy’s spawn. How are determining the range in which this random value is chosen? Seem difficult to me when there’s only one value to work with.
    Finally I just put a good word in for the Tooltip attribute (https://docs.unity3d.com/ScriptReference/TooltipAttribute.html). I find it helps make it easier for other people to interact with scripts I’ve written. It might help you as well when trying to improve accessibility for your designer 🙂

    Keep up the good work 😉


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s