Defend Mayberry!

My Unity 2D Challenge submission "Defend Mayberry".

Defend Mayberry!

Defend Mayberry and save the town from an alien invasion.


As part of the Unity 2D challenge, I was looking to learn and experiment with several of Unity's 2D packages in my second Unity Challenge, the first being the #NeonChallenge.  

Game Synopsis:

Each new game will have it's city populated dynamically, so no two city layouts are exactly the same. The player will use a combination of Up/Down/Left/Right keys to move their vehicle through the streets. With a mouse, the player will target and  launch missiles to blow up enemies before they destroy the town's buildings or the player. A counter at the top will show the number of buildings/cars in the town that remain, as well as the number of Aliens left to kill.. Fuel and Health for the player are also UI elements to watch. Aliens are sent in waves to optimize performance, but also not immediately overwhelm the player.

Assets and Tools

For the 2D Challenge, my main focus was using Unity 2018.3.x, 2D Cinemachine,  2D Animation package, and 2D IK for animating sprite based enemies.  As a patreon and follower of #Brackeys, and #Sykoo, the content those guys put together is awesome,  so much credit to those guys for their great instructional videos. I leaned on them heavily to learn how to do a lot of the basic code mechanics for moving sprites around, creating sprite animations, etc. Since I'm primarily a programmer, my custom art will be limited, but I created a few sprites to use and animate. (an Alien character sprite, a missile launcher on the player's vehicle, and a layered reticle that I'll animate). The Asset Store 2D City Sprite collection will serve as the main game world art, and I'm also incorporating D Sprite fire, and 2D Homing missiles.

I utilized the Cinemachine's Confiner extension setting, which allows you to set up a BoxCollider2D as its boundary. It keeps the player bounded inside of a specific width world, as well as fixed in the vertical. This Unity video was very helpful in showing how to set it up.And Post-Processing V2 as an extension on Cinemachine to slightly amp up sprite colors via color-grading.

I really enjoyed working with the new 2D animation and 2D IK packages.  My game characters are essentially the Truck, and an Alien. The Saucer was a couple different layered sprites.  I watched a few very cool videos from Brackeys and Sykoo, and also this Unity vid as well.   My Alien and it's Saucer (before rigging with in the Sprite editor:

Here's the Alien in it's Saucer, after creating bones for the Arms/Gun, and a few animation steps for moving the Arm around.. The 2D IK package really made it exceptionally easy to setup the bones with IK, and use the effector that it generates, as the way to setup the animations. There's no way I'd have had the patience to drag each of the bones individually.

After creating some animations, I used some script on the Alien Saucer to keep track of and trigger certain animations by using Animation Events. Those called code in the same script to receive and set an _armState value, which is used in this Co-routine to randomly move the arm around, tell the Fire to turn on and off, etc.

I gimp'ed up a Targeting Reticle based on some inspiration I found online. There's 4 layers here that are animated in Unity when the OnMouseOver event is fired from the Aliens..

In "Defend Mayberry!", there's the typical code to capture keystrokes handling the player and mouse cursor, which isn't that interesting to look at, so doesn't seem important to include..Most of my code/script setting up the game world, animating in various objects, and tracking damage.  e.g. Alien fire hitting cars or buildings, and instructing fire and smoke prefabs to activate and play at certain damage levels.One technique I used a ton was Scriptable Objects as a way to handle Events and manage global variables. There's a great link here.  I actually got to sit in on this session in person in Unite Austin.. loved it!   In addition, some new things learned (at least for me)... Sprite Sorting Layers and order values.. I utilized them heavily as a way of ensuring that everything was view ordered correctly..and Nested Prefabs, yesss!  All the buildings, cars, and Alien/Saucers are setup as prefabs that contain the sprites, colliders, script, and under them, the separate audio sources, fire and smoke prefabs with their own script..  It was nice to be able to tweak properties of the fire and smoke and just see them globally applied without having to go back to each object. Cool!    One last thing to mention, just because I put it to use in a few areas of script that required synchronized scaling between two different ranges of values.. For example, as health of a building or car goes from 100 to 0, I wanted to scale down the Color RGB values from 1f to 0.4f (to give a charred effect as damage occurred). Similar for the Player's engine sound as the speed increases and decreases, the pitch of the audio changes between 0.5f to 1f.  This link was very helpful.