Destroying an Object to Start the Game — Part 2
Last article we made our Asteroid Game Object, created a new Explosion VFX and created the basic logic for interaction. This left some bugs, but they are easily fixable.
By the end of this article, destroying the Asteroid will now officially Start our Game and start spawning Enemies!
Squashing some Bugs
We left off here in the last article:
The first issue is that our Laser does not get Destroyed when hitting our Asteroid. The Second is that there is a delay between the destruction of the Asteroid and the Instantiation of the Explosion VFX. The Third is that the Explosion sits behind the Enemy Ships, whereas it should be upfront and very noticeable!
(Yes I know there will not be any Enemies surrounding our Asteroid at the start of the Game, but it’s good practice, and we might want to re-use this Explosions VFX for something else!)
To fix problem 1 and 2, we use some simple code like this:
The first destroys the Laser. The second delays the destruction of our Asteroid by a little to time it with our Explosion VFX!
To fix our third problem we just have to change our Explosion_VFX order in layer:
Now our Asteroid should look a little better, like this:
We do have another problem as well — when we instantiate our Explosion_VFX, we never destroy it. So it lingers in our Hierarchy, taking up memory and space. To remedy this I created a new script where it’s only purpose is to destroy the object it is attached to after a set amount of time:
And the logic inside is simple:
Changing our Start Spawning Method
Now for the juicy Game Starting Logic.
Currently in our SpawnManager script, we have two Coroutines being called at the Start of the Game to initiate the Spawning of Enemies and Powerups.
To utilize our Asteroid to begin these processes, we’ll Start these Coroutines inside another function which we’ll call outside of SpawnManager and inside our Asteroid script:
Now inside our Asteroid script, we’ll get a reference to our SpawnManager:
And when a Laser collides with the Asteroid, we’ll then call the StartGameSpawning() function:
If all went well, we should have something looking like this:
While our Start Game logic is working, I don’t like the feel of how rushed it seems after the laser destroys the Asteroid — we barely have time to appreciate the Explosion we just created!
To fix this we can add a new WaitForSeconds inside our SpawnEnemyRoutine inside of our SpawnManager script. Adding this before our while loop will only call this once, at the beginning of our Coroutine:
And now with that little bit of delay, all feels right in the world again!
We’ve got a cool new mechanic for starting our Game, that gives the Player some time to get a feel for the controls, and makes them realize they need to shoot to survive!
In the next article we’ll be looking at adding some VFX to our Player to give them some graphical overhaul!