Bombs away! Adding another enemy to the game.

So last time we made the player have to think a little by adding a PowerDown that causes his movement to slow down by half. This time, we are going to add an extra enemy — a bomber.

I found a cool bomber looking spaceship and a bomb online and added them to my sprite list. I am going to use the explosion animation and sound when the bomber is hit.

This wasn’t at all that difficult to be honest. Once you have a lot of your code done it’s just adding a line here or there.

To start I turned the ship and bombs into prefabs and added colliders and rigidbodies. I then added a Bomber script and a Bomb script and attached each to it’s obvious object.

First, the Bomber script — all I did was add a CalculateMovement function that would take the bomber from the right to the left and then back again.

This was a bit tricky because it would go to the left and stop at the -10 on the x and then do nothing. I couldn’t figure out why but I knew it had to do with making it stop moving to the left before I could make it move right. Create a bool! Bingo — when _movingLeft was true it would move left, once there it would turn that to false and then move right.

To make it drop bombs I had to set the _canFire variable to the time of the game so that it wouldn’t just pour bombs out. I then checked that if the bomber was not null (I don’t want it dropping bombs when it’s destroyed) and if so gave it a fire rate of 1–3 seconds and then instantiated the bombPrefab I created.

The other code that I placed in here was just if(other.tag == “Laser”) and other objects that if they collided with the ship it would destroy it and set off the explosions and sound. Move of this can be found when I created the original enemies in past articles.

Inside the bomb script (above), I didn’t have to use any movement because I just left the gravity on 1 with the Rigidbody. You can play with that as you go — for example maybe the first wave the bomb doesn’t drop as fast as the 3rd wave. I’m still testing as I go but left it at 1.

In the Update I just have Unity check if the bomb drops off the bottom of the screen to destroy the object. Then a OnTriggerEnter function that checks of the bomb hits the player it should reach out to the player script and call the damage function. Once it has done that it should destroy itself.

The other thing I did was have it hit the enemy. I want it to be somewhat real so I just have it hit the enemy and destroy itself but not actual damage the enemy. You could of course — I left enemy lasers on damage allies, but in this case I didn’t want that. A cool thing would maybe be it hits the enemy and bounces off and sends the enemy on a different course. Ahh, but if there was more time. I don’t want to get into physics too much at this point.

Now, the last thing I did was add this bomber to the SpawnManager script. It was pretty easy to do. I just put this these lines of code inside of the SpawnWaveRoutine (I won’t put all of the old code it would be too big).

Basically this is inside of a Coroutine that is set to go off faster each wave and spawn enemies, asteroids and now, bombers. The above code — I wanted the bomber to spawn on the right, off screen and at the top of the screen. I gave it a random number and said that if a 1 is chosen then instantiate the bomber. It gives a nice number I find.

Well, let’s have a look how this plays!

Pretty cool! That initial enemy spawning right at the beginning really throws the player for a loop — and then the slow down PowerDowns… as the french would say, ohlala!

Now I have played this a little bit and have noticed that maybe the spawn rate of some things isn’t quit spot on. I’m going to tackle that tomorrow and recode the spawn rate of both the powerups and enemies and see if I can get a nice middle ground. Hope you join me!

Happy Clicking!

With Unity all can be accomplished!