Let’s Learn Unity – Laser Defender

Let’s Learn Unity – Laser Defender

For information and navigation, check out my post that introduces this Unity series here.

TL;DR? Skip to the game.


Reflections on my Scheduling

Before we start, I would like to go over the results of my scheduling so far. The main aim of announcing the dates of my blog posts publicly and ahead of time was to force myself to have some accountability (a Tim Ferriss trick). Fortunately, I don’t seem to have any problem uploading my posts, but uploading them on time is another matter. Now, while it is true that being even more accountable might fix this, I think that the best approach is to develop a better understanding of what’s going.

In each of the three weeks since starting this series. If I had started writing my blog posts on the day they were due (or even the day before), then I would have had a fully working game to go with them. However, as is often the case for programmers, I always found myself wanting to add a little bit extra, venturing further and further from the Minimum Viable Product (MVP) that I discussed in my last post. The only time this was not the case was for Number Wizard, which also happened to be the only time that I didn’t go beyond the MVP.

I have learnt a lot from doing this, but meeting deadlines is an important skill too. That said, I am resolving to have this week’s Glitch Garden post online by the scheduled time (Saturday 28th October 2017).

Here is some more info on accountability from Tim himself, including another example using dieting.

Now, on to Unity!


This Week

In this week’s section of the Udemy Coursewe are creating a game called Laser Defender. The main elements of this section are a Player Ship, some Animated Enemy Ships and their respective Projectiles, but it also has units moving as a group, a more advanced music system, menus that fit in with the feel of a space shooter (see below), and finally a score.

Indeed, Laser Defender is very much reminiscent of Space Invaders.

The caption says it all.
Start Menu (left) and Lose Screen (right).

The animations in this game were a bit basic, so I am hoping that I will be able to go into more detail next week after I have learnt some more!


Laser Defender

I diverged from the course every now and then and added a few extra elements to my game. I will go over what I did for my Sprites, Music, and Health Bar below:


As I have never done pixel art before, I looked for some artwork online. Luckily, I managed to find a whole collection of different spaceships made by Skorpio on OpenGameArt.org. I took my favourite two, a blue one for the player and a red one for the enemies. Later, I wanted the sprites to briefly change colour when hit by a projectile to make it more obvious that they were receiving damage.

You can create a good looking effect using Gimp. Go to Layer > Transparency > Alpha to Selection to select your sprites, then Colors > Map > Color Exchange… and you can play around with the colours of your sprites until you have them looking the way you want. See my final sprites below.

Normal sprites and sprites after a colour exchange.
Player (top) and Enemy (bottom) Sprites, normally (left) and while being hit (right).

I also added two explosions, made up of three particle effects, for when the player is hit and an enemy is destroyed. In keeping with the colour themes, I used Dark Blue, Purple, and Light Blue for the player and Red, Orange, and Yellow for the enemies.



In the course, we are taught how to have a different piece of music playing in each scene. However, I wanted my Menu Screens to reflect the silence and isolation of space and then use music to emphasise the action of the gameplay.

So, I took three tracks (Depth Charge, Heavy Hitter, and Meat Grinder) from the Rock collection of Purple-Planet.com‘s royalty free music and set one to play randomly whenever the player starts a new game. The player could then scroll through the tracks or mute them by pressing M.


Health Bar: 

In my game, the player can be hit up to five times before dying. It can be hard to keep track of how many times you have been hit, so I added a health bar.

To do this, I simply made two sprites: a frame and a white box. The frame remains the same size and is filled with the white box. The white box’s colour and size are controlled by the following function within the player script:

private void UpdateHealthBar() {
    healthBar.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, health * healthBarMaxLength);
    if (health < 3) {
        healthBar.GetComponent<Image>().color = new Color(249 / 255f, 75 / 255f, 75 / 255f);
    }else if (health < 5) {
        healthBar.GetComponent<Image>().color = new Color(249 / 255f, 172 / 255f, 75 / 255f);

This function is called everytime the player is damaged. In my game, max health is 10. So, the bar turns orange when below 50% health and red below 30%. Otherwise, it is blue.

The RGB colour values are divided by 255f to convert them into floats between 0 and 1.

Note: If you are using C# 6.0 or later, you can declare “using static UnityEngine.RectTransform;” at the top of your script and then change “RectTransform.Axis.Horizontal” to simply “Axis.Horizontal” if you wish.


Play my Game Here!



If you want to take your Laser Defender game even further, then, as usual, I have some ideas left over:

  • Add multiple types of Alien ship and/or projectiles (suggested in the course).
  • Display the name of the music playing on screen (especially if you have changeable music).
  • Have asteroids and other obstacles drift down the screen (or have debris left over after destroying an alien ship).
  • Include an “overheating” mechanic to prevent the player from firing constantly.


Feel free to use any of my ideas or let me know if you have any more of your own.

Share your games with me at @TheLearnJourn, and use #GameDev to share it with GameDev.tv, they really do enjoy playing them.

Keep Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *