2D Galaxy Shooter — Shooting an Asteroid to Start the Spawn Manager!

Marcus Cohee
4 min readAug 26, 2021

--

Hello all!

Today, I will go over how to make an asteroid that spins in place, explodes, and starts a war with the enemies! I hope this pairs well with the Starting_Scene I made in an earlier article.

Making the Asteroid with an Explosive child

  1. Find a good asteroid sprite, then drag it into the hierarchy and rename it Asteroid.
  2. Give it a Circle Collider 2D component, set the “Is Trigger” to true, and make the Hitbox.
  3. Then add a Rigid Body 2D and set the gravity scale to 0.
  4. Find an explosion animation sequence and drag its first frame on screen and rename it Explosion.
  5. While selected, go to the animation window, then hit the create button.
  6. Name the animation: “Explosion_anim”.
  7. Now hit the record button and drag in the sprite animation sequence onto the DopeSheet.
  8. Go to the Animation, make a new animation block, and name it Empty.
  9. Right click Entry and hit “Set StateMachine Default State” and drag the arrow onto Empty.
  10. Right click Empty, hit “Set Transition”, and drag the arrow on the Explosion block.
  11. Create a “trigger” parameter, and name it “OnAsteroidDeath”, and uncheck the circle beside it.
  12. Click the arrow between Empty and Explosion, then add a condition of OnAsteroidDeath.
  13. Set the Has Exit Time to false, then set the other values to 0 below it.
  14. Test the animation and watch when the asteroid is engulfed in flame. Keep that time, you will need it soon.
  15. Center it on the asteroid by making them both 0x, 0y, 0z.
  16. Make the explosion sprite a child of the asteroid.
  17. Now prefab the asteroid.

Getting the SpawnManager Script ready

  1. Open the SpawnManager script.
  2. In the Start method, move the two StartCoroutines to a new method called “public void StartSpawning()”. This will be referenced in the next section.
  3. In the Coroutines below, add a “yield return new WaitForSeconds(2.0f);” right before the while loop.

So when the asteroid is destroyed, the enemies don’t flood the screen immediately, they will have to wait 2 seconds before spawning.

Asteroid Behavior

This will go over how to make it spin, allow it to be triggered by the laser, and a coroutine for the explosion.

  1. Create a new C# script and name it Asteroid.
  2. Create variables:
    [SerializeField] private float _rotateSpeed = 20.0f;
    [SerializeField] private Animator _onAsteroidDeath;
    [SerializeField] private SpawnManager _spawnManager;
    - Link the Spawn_Manager to the Asteroid script.
  3. In Update, add the code below. It’s saying that it will rotate forward at 20 speed per second.

4. In Start, add the code below. It’s pulling components from the Explosion animation sprite and the Spawn_Manager script then null checking them.

5. Create the OnTriggerEnter2D and IEnumerator method below. This is saying that when hit with a laser, then destroy “other” object, which is the laser, then set the OnAsteroidDeath condition which starts the explosion. Going off the last section, it will start the “spawning Coroutine”, which will wait 2 seconds before happening, then start the asteroid’s own coroutine. AsteroidExplosionRoutine will wait for 0.2 seconds, turn off the Circle Collider 2D and Sprite Renderer, making it invisible, then waiting for 1.8 more seconds before destroying the asteroid.

The reason I set the coroutine up like this is because after 0.2 seconds, the asteroid is completely covered by the explosion, so we turn it invisible to make it seem blown up, then the 1.8 more seconds are because the animation is 2 seconds long.

Test out the game. It should allow you to shoot the asteroid, wait for 2 seconds, then begin the enemy’s descent!

That was how to make an exploding asteroid that starts our Spawn_Manager! Next, I will go over thrusters and damage VFX for the player.

Thank you for your time!

--

--

Marcus Cohee
Marcus Cohee

Written by Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!

No responses yet