Implementing a Way to Control the Spawn Manager in Game

Brian Ratnasinghe
3 min readJul 2, 2021

--

Before implementing the following code the game would simply start as soon as the scene was instantiated. Here, I add an asteroid to the game that when destroyed initiates the spawning of enemies. This will hopefully make the game feel more fluid and make the player feel as though they have more control.

I created the Asteroid object using a pre-rendered sprite and added a Circle Collider 2D, a Rigidbody 2D, and a Behavior script called “Asteroid”.

Next script component was edited to initialize a reference to the spawn manager, and to respond to being hit by a laser using an OnTriggerEnter method. This method is coded to destroy the asteroid Object, destroy the laser, and begin the spawning sequence by called a method with in the spawn manager.

Besides starting the spawning sequence the OnTriggerEnter method also instantiates the explosion prefab, which is one of our cool visual effect animations. The object is simply an animation that is had a script component to destroy itself shortly after being animated.

The Spawn Manager is where things get complicated. The StartSpawning method begins two different coroutines. One of which is responsible for spawning enemies and the other is responsible for spawning powerups.

The way both methods works is that they instantiate the game objects after a number of seconds above the top of screen a random x-coordinate. After spawning these game objects move downwards due to transform calls within the update methods of their script components.

That it, here is the final result:

--

--

Brian Ratnasinghe

Here I am documenting everything I am learning in order to become a successful game developer.