Space Shooter: Enemy Spawning

I’m able to fire a laser now and move around. What am I shooting at?

In this article I’ll go over setting up a simple spawn manager that instantiates an enemy prefab at a random x position and when that enemy reaches the bottom of the screen it moves back up to the top at a different location on the x axis.

First I need to create an enemy. This means I need to define what I want the enemy to do. For now, I just want it to move down the screen and when it’s Y axis reaches a certain value make it reappear at the top of the screen.

So to do this I need to create a cube, zero out its position, and create an Enemy Class.

Inside my enemy class I have created a Movement() that is called in Update.

First things first, I set a float variable called randomX equal to Random.Range(-15f, 15f). Random.Range allows Unity to choose a number between those two values. For this situation I want the enemy to soawn at a new random x location when they appear back at the top of the screen. So the transform.Translate() is telling the object to move down and if the y position is less than or equal to -10f, the new position of this object is now at a random place on the x axis, 9 on the y and 0 on z. I’ll get into this hit bool later. It basically delays the time it takes for this object to destroy after its been hit by a laser.

So now the cube should move down the screen at whatever speed is set in the inspector.

But I want more than 1 enemy. So this is where a SpawnManager can be handy. I’ll create a Spawn Manager game object, zero out its position, and a SpawnManager Class.

Here are the variables I’ve set up. 1 for the enemy game object and one game object for a container. I’ll explain dont worry.

I’ve created a coroutine to handle spawning. This basically says while the player is alive, the position to spawn is a random value on x axis, 9 on y, and 0 on z. Then I’ve made a GameObject variable called newEnemy and it is equal to the instantiation of the new enemy game object. I did this so that I could set it’s parent to the enemy container.

If I were to have just left the instantiate method there without casting the newEnemy to a new GameObject type I wouldn’t have access to it’s transform.

Then the yield return new WaitForSeconds(2.5f); line is what makes this coroutine work. This makes the application wait to call this IEnumerator again for 2.5 seconds. SO every 2.5 seconds I’m instantiating a new enemy game object and setting its transform equal to the enemy container transfrom.

The enemy container is an empty game object in the hierarchy that is a child of the Spawn Manager. I’ve set it up this way in an effort to keep the hierarchy clean. Perhaps later I’ll have different sub folders within that to spawn different enemies and house them in their own containers. Organization!!!

I forgot to mention that I also have a public bool playerDied; in here. this is what determines whether that coroutine continues to loop.

This function lives inside the spawn manager as well. Its called in the Player class after colliding with the enemy 3 times. I’ll get into collision in the next article, and then I’ll dive into spawning a triple shot power up for the player. For now we have a simple spawn manager set up that spawns a bunch of cubes!

If you’re stumbling upon this article, but have no idea what I’ve done to get to this point, feel free to checkout the other articles in this list! Until next time. :)

--

--

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
Gerald Clark

Gerald Clark

Father Game Developer Music Composer Sound Designer