Destroying an Object to Start the Game — Part 1

Frank Warman
4 min readMay 4, 2021

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.

Let the Game begin!

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).

Static Image of our Asteroid turned into our GameObject.

Then to fix the collider so that it fits our Asteroid:

Fixing the Circle Collider

To be able to give our Asteroid some Logic, we’ll create a new C# script, and attach it to our Asteroid Game Object:

Creating and attaching a new script.

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!

Rotation Logic

And making sure it works:

Asteroid Rotation example

The next issue we’ll tackle will be making the Asteroid react to our Player’s Lasers, using OnTriggerEnter2D().

Explosion VFX to come.

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:

New Game Object

And create the Explosion Animation utilizing the other sprites:

Creating a new Animation

Which should look something like this:

Kaboom!

Now to be able to Instantiate this VFX properly, we’ll make it a Prefab!

Prefabbed!

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:

Instantiate BEFORE destroying the Asteroid, or it will never be called!

Linking the VFX Prefab to our Asteroid Script:

You can delete the current Explosion_VFX in the Hierarchy, since we will not be using it now.

And now our Asteroid has an awesome new Explosion!

Asteroid 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!

--

--

Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles