Defend Mayberry!
Updated 10 months ago
Defend Mayberry and save the town from an alien invasion.
Final Video with a bit of 'making of..' for submission (12/14/2018):


Hello all, I'm looking to learn and experiment with several of Unity's 2D packages in my second Unity Challenge, the first being the #NeonChallenge. With the Neon Challenge, Cinemachine ended up being one of my favorite new features within Unity, so with this 2D Challenge, it'll definitely be part of my submission. I'm primarily using Unity to develop AR and VR applications, and haven't tried creating a true game or anything in 2D until now. I spent countless hours (and quarters) on the old-school arcade games Defender, Asteroids, Missile Command, Moon Patrol, and many others. With this 2D challenge, my plan is to create something of a hybrid of all my old favorites.

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 will be on 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 I give much credit to those guys for their great instructional videos. I'll be using and relying 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 a programmer, and only an aspiring artist, my custom art will be limited, but I'm creating 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 2D Sprite fire, and 2D Homing missiles.

Development Notes:

My submission uses some asset store art that matched the look/feel I'm going for.. better than I'm capable of creating at the moment.. kind of reminiscent of a higher res 80's or 90's arcade game.. For me, the real gist of the challenge though is to use Unity's new 2D tools to create something cool. I already know Cinemachine is a great tool as it elegantly solves the problem of having the camera in the right place ALL of the time. Smooth player tracking using a Cinemachine virtual camera is my focus for it's use in this game.
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. As someone who has next to zero animation and rigging skills, I was relieved at how easy they were to use. 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.

Post Challenge thoughts:

I'd like to try to get a real game out there in Play store or Apple store, so I've got some ideas that I may try to work on.
  • My Gameworld width can be scaled dynamically, so perhaps start with a smaller level, and just a few aliens, with additional levels that increase the world width and more aliens..
  • When Aliens die, they should drop some metal, or fuel - as pickups for the player to refresh damage and gas.
  • Perhaps a power-up for the player to pick up that will allow auto-fire for a few seconds on all enemies in the camera view...
  • I'd like to add a boss alien, something similar to a walker for the main street, that would require more damage to kill.
  • Some kind of reward element for killing all the Aliens before they destroy the town, and ability to use that as currency to change the player's car to a different color or type, or maybe different backgrounds... Anyone got any ideas?
  • Incorporate touch input for mobile. Something similar to mobile minecraft perhaps for the L/R/U/D, and a secondary touch for locking an enemy for the missile...
  • and.. maybe some IAP or Ads for continue game when a player dies.. That's an area I need to look at more closely in Unity..
  • This challenge was FUN! It was good to checkout and get some experience with a few of the new 2D tools, particularly (for me).. Animation and IK.
Rod Ellison
Staff Engineer - Programmer
Joao Milone
a year ago
Indie Game Dev - Programmer
yeah, the implementation of that was flawless
Rod Ellison
a year ago
Staff Engineer
Joao MiloneNice animations man, they really help keep the game immersive, especially that truck :)
Thanks Joao, - having a bit of fun with them.. ..and the IK thing was much simpler than I thought it would be for that gun under the spaceship.. planning to have that gun target houses and the truck and switch between their transforms to get the gun to reorient automatically using the IK Effectors..
Joao Milone
a year ago
Indie Game Dev - Programmer
Nice animations man, they really help keep the game immersive, especially that truck :)