Let’s Learn Unity – Glitch Garden

Let’s Learn Unity – Glitch Garden

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


 

Plans For The Future

Glitch Garden marks the halfway point in the Udemy Course. It is also the final project before moving on to 3D and the first one that has taken me longer than a week to finish. This extra complexity has allowed me to inject some of my own inventions, and although I have finished its Minimum Viable Project, I would like to spend a bit more time on it before releasing it to the public.

Additionally, I have two other things that I would like to finish before learning about Unity 3D. In the meantime, my scheduling will be going out of the window! Even more than before.

Now, in this post, I will be updating you on most of what I have done to Glitch Garden so far. But the game itself won’t be available for at least another week. At which point, I will also share it on my Twitter and Instagram! So stay tuned!


 

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:

  1. I could change my trophies to act in the same way as Bens’. But this would undo all my previous work.
  2. 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.
  3. 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.

Some colourful rocks!
My ‘Oreful’ Rocks

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.

A dwarf digging a hole!
Diggy Diggy Hole!

 

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.

  1. I could go back and add in gravestones. This would be very easy and require little work.
  2. I could remove the foxes’ ability to jump. This would be even less work but would result in sad foxes.
  3. 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.

The fox jumps over one of each obstacle.
A fox, jumping through an obstacle course.

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.

The spikes rise up through the floor.
Spike Trap entrance animation.

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.

Rabbits getting shredded.
Rabbits being chased over spikes by a fox.

 

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).

It hits a cactus, it breaks, repeat.
A lizard beating up some cacti.

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

They run inbetween the pots.
Run rabbit, run rabbit, run run run.

 

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.

A big ugle disk and a bit of white light.
Body on the left, light on the right.

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.

The lizard walks onto the mine and they both disappear.
Beep, beep, beep…

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.

  1. An attacker spawns.
  2. The defender detects the attacker; the trigger is activated.
  3. The defender’s attack animation starts, deactivating the trigger.
  4. Before the attack animation ends, the defender detects the same attacker and re-activates the trigger.
  5. The defender attacks the attacker; the attacker is destroyed.
  6. The attacking animation ends.
  7. 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 I mentioned at the start of this post, I have a few more things that I would like to add before releasing my version of Glitch Garden. Here’s a list of a few of them. Feel free to borrow these ideas and let me know if you have any others.

  • Sounds.
  • Change attacker sprites to show when hit (as with my Laser Defender).
  • Add additional themed levels.
  • Land mine 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.

Leave a Reply

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