Space Shooter Challenge: New Enemy Type

Calum Slee
Geek Culture
Published in
4 min readJun 15, 2021

--

My next challenge was to create a completely new enemy type, with both a unique projectile and movement behavior. So of course, I gave myself the hardest challenge I could and created the enemy you see above. It took various different elements to piece it together, and this article will do it’s best to show an overview on each.

First, I had to choose an enemy. Much like my missile, the enemy I found was a 3D object. So I imported it to Blender and added the materials so it looked just like the Prefab I downloaded in Unity from Filebase. I then removed the missiles that were on it by default and found a new missile on Filebase I could use.

I wanted the Enemy to do a barrel roll, which meant rendering various rotations so I could use animated sprites in my 2D game. As each missile is fired, I also needed these animations to follow suit. So I rendered one for each iteration of missile count on the wings.

Now I could start in Unity. I copied my Enemy Prefab and changed the sprite to be my new enemy. I wanted to make use of many of the behaviors I had already established, and therefore used the same Enemy Script. Assigning a new ID of 3 to the enemy, I could add a new case to my IDBehaviours Switch statement. I also moved the call to Start the FireLaserCoroutine, to each appropriate case instead of the Start method. Then in case 3, I could start an alternate Coroutine to take care of the new enemy behavior.

I also changed my Update method so the Movement method wouldn’t be called for this new enemy type, using another switch statement.

Now for the MissileEnemyRoutine. I wanted the enemy to travel until a certain point when spawned, before stopping to fire it’s missiles and do it’s barrel rolls. So I started with a while statement that allowed the Enemy to move as per normal.

Once this loop had finished. I wanted to fire a missile, before barrel rolling the opposite direction, and repeating. I converted my weapon Prefab Game Object variable to an Array, adding the Left and Right Missiles that I had set to the new enemy prefab. Then I created a new method called WeaponAssign as seen above in the IDBehaviors, this method checks the tag of each object in the Array, and if it’s a missile, adds it to a List, similar to the Player Missiles.

Back to my Coroutine, I could check if this List was populated, and while so, fire a missile. I wanted to pick a side to fire randomly, again, much like the player, but I also wanted to barrel roll the opposite direction, so I would need to know which side was fired. Therefore my FireMissile method would return a string I could use for the barrel roll.

The FireMissile method was very similar to the same method in the Player script with the addition of getting and returning the name as a string.

So far, I had an enemy that would travel down the screen, stop, then fire both of it’s missiles.

To create the barrel roll, I created various animations, then in the Coroutine, I could pass the string variable into a BarrelRoll method that returned a Vector3, which I would use to later move the enemy horizontally while the animation occurs.

Then while the animation was occurring, back in the Coroutine I could move the enemy horizontally in the appropriate direction.

For the animator to work, I had to create two separate paths, that the triggers called, would set. This took a lot of experimenting with fade times and disabling loops etc, as I’m not well-versed when it comes to Animation control. Something to learn more of in the future!

Lastly, I needed the enemy to leave the screen. As it had no means of fighting anymore, I wanted it to disappear quickly. So after the missile count reached 0 and the while loop ended, I started a new Coroutine called EndMissileEnemyRoutine.

In this routine, I enabled the thruster, and it’s audio. Then while the enemy Y position is still onscreen, I moved the enemy downwards at double speed, then I could destroy the object.

--

--