Creating Enemies

Kenny Pruitt
Unity Coder Corner
Published in
5 min readFeb 27, 2023

Objective: Create A Basic Enemy Object & Enemy Spawner

To create a basic enemy we will start with the same way we made the Player object. Create a new cube in your scene then attach an enemy model to the cube and adjust the scale and rotation so that it is facing the opposite direction of the player.

Now that we have a basic enemy model we can make it a prefab and create an enemy spawner to spawn enemies. To do this, in our Hierarchy right click — click Create Empty.

We can use empty objects as a type of game manager. Then create a new C# script for an EnemySpawner. On this we will spawn an enemy above the screen so they can fly down. So first we can make Enemies spawn at random locations on the x axis so they won’t fly down from the same spot. To do this we can use the Random.Range function to randomize between 2 float or int values.

Then we can create a spawnPoint Vector3 that uses that Random.Range function to spawn an enemy in a random x position and then spawn an enemy. I also made it so the enemy will spawn above the view of the screen by adjusting the y position of the transform component.

Then we need to make the enemy fly downward after they spawn. To do this we can create a new C# script for our Enemies and place it on our Enemy prefab. Then we can create a downward movement for them by using transform.translate function.

Then we can create a bool for if the enemy has escaped and not killed by making it so once the enemy has reached below a certain y transform.position. It will destroy the enemy, but wont register later as being killed by the player.

Then since we want to register if the enemy has been killed, we can add 2 ints for enemiesSpawned and enemiesKilled. And each time an enemy is spawned just add 1 to enemiesSpawned, which can be written just like this to add 1 to itself.

The last thing we need to do is add a cooldown for the spawnRate. To do this we can simply make a cooldown function that is triggered after each time an enemy is spawned.

I also am getting the reference to each enemy spawned so I can access the Enemy script on each new enemy spawned so that the enemy can communicate back to the EnemySpawner if they have been killed.

Once I have the reference to the gameobject I can then get access to any script on the gameobject by using GetComponent<ScriptName>(). Then I can adjust public variables on that script from this script.

Now to cleanup our code a little, we can replace the cooldown function with a Coroutine and also add in a way to disable the spawner as well if we wanted. To do so we can make IEnumerator function and call it SpawnEnemies.

Then inside of this function we can make a While loop that will continue to spawn enemies unless a bool called DisableSpawner is true. If we use a “!” symbol before a statement, you are basically calling that statement false.

Next we need to determine the time frame of how often an enemy will spawn, for this we can simply use the function WaitForSeconds().

With this, the code will wait for a random amount of seconds before proceeding to run the code under it. Then we can just spawn our enemy like before. But we also need to start this Coroutine by saying StartCoroutine(“SpawnEnemies”);.

Just as a note, it’s a good idea to actually parent the enemies to a localized area for keeping the Hierarchy cleaner in Unity. To do so, first we can add a child object to the spawner and we will call it SpawnedEnemies.

Then when an enemy is spawned, we can simply make our newEnemy set the Parent to the enemyParent.

--

--

Kenny Pruitt
Unity Coder Corner

Unity Game Developer, C# Developer, and Software Engineer