For information and navigation, check out my post that introduces this Unity series here. TL;DR? Skip to the game.

Plans For The Future

Glitch Garden marks the halfway point in the Ben Tristem’s Unity course. It’s also the last 2D game. As such, I feel like it’s also a good place to take a break. As much as I have enjoyed this course, I feel that it’s time for me to move onto something different. The main reason I started this series was to learn and help others learn some basics about game development. I’ve certainly achieved this for myself, and I hope that I have helped at least one other person along the way.

The Text Adventure Game is nearly complete, so I will still upload that before moving on.

The Gold-Star Mixup

Those of you who are familiar with this Udemy Course will know, Glitch Garden is based on Plants vs. Zombies. Therefore, it might have been a good idea to play this game before starting the Glitch Garden section. Unfortunately, I didn’t think to do that before ploughing on ahead. Consequently, I had a bit of a hiccough when it came to the star trophies (our sunflowers). The purpose of these is to generate currency that the player can then use to buy/grow more defenders.

As I was blissfully unaware of this, I felt that Ben’s “star floats into the air” animation was a little lacking, and instead animated my star trophies to launch their stars at the oncoming assailants. Great for damaging attackers, not so great for making money.

I came up with three solutions to this problem:

I could change my trophies to act in the same way as Bens’. But this would undo all my previous work. I could simply have the attackers drop money when they died. But I didn’t think that this would be very rewarding for the player. I could come up with a completely new defender to generate currency. But this would be the most difficult and require the most work.

Naturally, I went for the third option.

Mining For Gold!

Sprites

At this point, I was still more or less making it up as I went along. So, I searched through OpenGameArt.org for ideas. Luckily, I managed to find a set of sprites for a dwarven miner. I put this alongside a furnace/smelter (found using Google Images) and some rocks and ores (from Google Images and OpenGameArt respectively). Followed by some editing using Gimp. I couldn’t decide on which rock to use, so I used three (see below). When a new gold miner is made, one of the three rock sprites is randomly selected.

Side Note: I am liking Gimp more and more each time I use it. Hopefully, I will find time to do a dedicated series on it in the future.

Animation

Luckily, this step was fairly easy. Obviously, the rocks and furnace weren’t doing much in the way of movement. The dwarf came with a complete sprite sheet, so I only had to add in the sprites I wanted and then translate his position as he walked towards the furnace.

So far, the animation was fine, but it was missing something. The furnace felt unresponsive, and it wasn’t always obvious when the gold was being created. To solve this, I added in two particle effects. The first was a bit of orangey-yellow appearing inside the furnace and the second was a plume of smoke rising out of the top. This was then followed by a gold ingot rising up and fading away.

The particle effects and gold ingot made a big difference, and it was now clear when gold was on its way.

Obstacles

In Ben’s version of Glitch Garden, the fox is able to jump over gravestones. However, I replaced the gravestones in my game with “lightning hands” (see last week’s post), and I didn’t like the idea of foxes being able to jump over my defenders. So my poor foxes didn’t have anything to jump over. Once again I was left with a problem that could be solved in a number of ways.

I could go back and add in gravestones. This would be very easy and require little work. I could remove the foxes’ ability to jump. This would be even less work but would result in sad foxes. I could go completely overboard and create a whole new subclass of defenders just to let the foxes jump over them. This would require the most work by far.

In keeping with the theme of making things difficult for myself, I chose the final option.

After brainstorming a few ideas, I settled on spike traps, cactus patches, and land mines. In game, these “obstacles” are cheaper and have a fairly limited use compared to the rest of the defenders.

Now my foxes are happy again!

Spike Trap

The Spike Trap is the first of my obstacles. Various types of spikey traps can be found in countless games and films, as well as in real life. So, it was easy enough to find a ready-made sprite sheet on OpenGameArt.org. This included a nice little animation to show the spikes entering the scene.

Unlike all my other defenders so far, attackers will not attack spike traps. Instead, they will simply walk over them, receiving damage over time while they are in contact with the spikes. The traps will then break after dealing a certain amount of damage.

Cactus Patch

Next up is the Cactus Patch, which I made using an image found using a Google Images search. This is my favourite of the three obstacles. Made up of three baby cacti, it can be used to slow down attackers allowing other defenders to deal more damage.

Each cactus breaks immediately when it receives damage, and then reduces the size of the cactus patch’s Box Collider 2D. This forces attackers to move forward and repeat their attacking animation for each baby cactus. Additionally, the cacti deal a small amount of damage back to the attacker when they are hit (I have also added this feature to the bigger cactus defender).

The space in between the cacti also happens to be big enough for rabbits to run through.

Land mine

Finally, we have the Land mine. I couldn’t find any images that I was happy with for this one. So, in the end, I made some myself, using Paint and Gimp. As you can see below, it is made up of two sprites, one for the main body and one for the light. I made the light white so that it was easier to change the colour in Unity.

Please feel free to download and use this mine in your own game.

The mine is easy to animate. All you need to do is position the light, choose the colour(s) you want, and then set the flash frequency by modifying the alpha value throughout its animation.

In its idle state, my mine flashes green every now and then. When an attacker is close enough, it will flash red for three short bursts and then destroys them both.

At the moment, the mine only destroys the last attacker that contacted it upon “explosion”. However, I would like to destroy multiple enemies if there is a close enough group of them, and perhaps also deal AoE damage to a wider area. Some sound effects and an explosion animation would be nice as well.

Grid Settings

As with Plants vs. Zombies, we don’t want to be able to place multiple defenders in the same place. To prevent this, Ben cleverly used each defender’s Box Collider 2D to “cover up” their space. Meaning that instead of clicking on our core GameObject (which would spawn a new defender), we were only able to click on the already placed defender (which does nothing). I tried this method, but it caused some problems in my version.

For instance, with the bigger Box Collider 2Ds, my foxes now jumped directly onto the obstacles instead of over them. This could be fixed by adjusting the attackers’ Box Collider 2Ds, as Ben did. But, if I changed the width of the Box Collider 2D of a Cactus Patch each time it was hit, then that space would no longer be entirely “covered up”.

So, I decided to add in a 2D bool array to keep track of the empty spaces. This was easy to implement because the snap-to-grid spawn method meant that there were only a limited number of possible free spaces: There are 5 rows with 8 columns, so 40 potential spaces in total. Whenever a new defender is spawned, the location of that defender’s position is set to false in the array. When a defender is destroyed, that location is set to true.

Now, all I had to do was look at the position in the array to see if it was free before spawning any new defenders. Problem solved.

Animator: Triggers vs. Bools

Another difference between Ben’s game and my own is that he used bools to determine when his defenders were attacking. When he did this, I thought that it would be more appropriate to use triggers, as the defenders would never be interrupted mid-attack. It turned out that Ben’s method was much better than mine, but hey, at least I’ve learnt from my mistake (hopefully).

Unlike bools, triggers can only be activated and not deactivated. This means that once an animation’s trigger is set, that transition cannot be cancelled (unless the GameObject is destroyed or the scene changes etc.). However, triggers deactivate themselves when the transition occurs.

The problem is caused because defenders check if there are any attackers in their lane at every update. This is quite hard to explain in regular terms, so here is some pseudocode that will hopefully make the problem clear.

An attacker spawns. The defender detects the attacker; the trigger is activated. The defender’s attack animation starts, deactivating the trigger. Before the attack animation ends, the defender detects the same attacker and re-activates the trigger. The defender attacks the attacker; the attacker is destroyed. The attacking animation ends. The trigger is still active, so a second attack animation is played (even though no attackers are left).

If bools were being used, then they could be deactivated (set to false) after step 5. Instead, each defender attacks an additional time after their lane has been cleared. This is not a major problem, so I have decided to leave it in, see if you can spot it happening when I finally release my game.

Challenges

As usual, here’s a list of a few ideas that you can think about implementing into your own version. Feel free to share any other ideas you have either below.

Extra sound (on hit, Attackers growling intimidatingly etc.).

Change attacker sprites to show when hit (as with my Laser Defender ).

). Add additional themed levels (snowy tundra, sandy desert).

Landmine AoE and explosion animation.

Health bars on attackers and defenders.

Give lizards the ability to spawn in the middle of a lane.

That’s all for now. I hope you have enjoyed this post and have found something to take away from it.