While we’re still on the topic of Explosions from the last couple articles, let’s implement one more! This time we’ll create an Asteroid that the Player will have to destroy to Start our Enemy Spawning!
Yes, we have a Main Menu now to start our Game, but we’ll be nice and give our Player a little more breathing room once the Game has been initiated.
Creating the Asteroid
If we’re going to destroy an Asteroid, first we must create an Asteroid:
I have a static Image that I’ll use for the Sprite Renderer. Then change our Sorting Layer, and add a Circle Collider 2D (Is Trigger is checked) and RigidBody 2D (Gravity Scale to 0).
Then to fix the collider so that it fits our Asteroid:
To be able to give our Asteroid some Logic, we’ll create a new C# script, and attach it to our Asteroid Game Object:
Inside the script
The first thing we’ll add to our Asteroid script is to give it some Rotation so that it stands out the Player, enticing them to shoot at it!
And making sure it works:
The next issue we’ll tackle will be making the Asteroid react to our Player’s Lasers, using OnTriggerEnter2D().
A new Explosion VFX
Our Enemy explosion had a specific explosion that incorporated the Enemy ship sprites into it’s design. This new explosion will simply be just an explosion, and we can then instantiate it on the Asteroid’s position rather than making it a part of it’s Animator Controller (not that the Asteroid has one anyways).
So we’ll create a new Game Object that will become our Explosion_VFX Prefab:
And create the Explosion Animation utilizing the other sprites:
Which should look something like this:
Now to be able to Instantiate this VFX properly, we’ll make it a Prefab!
Back into the Asteroid Script
Now that we have something to Instantiate when the Asteroid is destroyed, let’s finish off the logic!
Create a reference to our Prefab. [SerializeField] so that we can link in the Inspector:
Then Instantiate the VFX when a Laser collides with the Asteroid:
Linking the VFX Prefab to our Asteroid Script:
And now our Asteroid has an awesome new Explosion!
As you can see it’s not totally perfect — for example above, the Laser is not destroyed and passes through, destroying the next Enemy behind the Asteroid.
In the next article we’ll finish off our Game-Starting Asteroid, by fixing some bugs and changing our Spawning Logic!