This demo is my first real attempt at making something in proper 3D using Unity and follows on from the 2D Asteroids style game I made back in January. As with that earlier demo, I used C# to write all the main game scripts, while the graphics and sound effects are from downloads available from the Unity Resources section of the Unity Technologies website.
My aim was to make something reminiscent of classic on rails shooters like Starfox, Afterburner, Rez and the Panzer Dragoon series. I spent quite a while looking at those games and trying to mimic the controls and camera behavior for this demo, and while it’s clearly not a patch on those games, I guess there are a few similarities in places. If you would like to play the demo please click the image above.
A bit about how this demo was put together
The game is split into two scenes, one for the title screen and one for the main gameplay, each scene has a controller script attached to it’s main camera which keeps an eye on everything in that scene. The game scene uses a number of prefabs for things like the starfield, the asteroids, lasers and explosions, the controller then attaches these at runtime with a degree of randomness to make the game a bit different each time you play.
The player ship is made up of a number of nested GameObjects to allow it to move forwards along the Z axis, while also freely moving on the other two axis as well as tilt and rotate to follow the users mouse position for aiming. This was probably the most complex part of the game to work out and took a lot of tweaking to get it working just right, but the outcome adds a nice floaty yet responsive feel to the craft.
The game’s main camera has two different modes, each controlled by a different script. The gameplay mode uses a variation of the orbit camera that comes with Unity to loosely follow the player ship. The movie mode continues to target the player ship while rotating around it at varying speeds, this mode was used for the intro and game over sequences as well as the short intermissions between each wave.
The music is made up of a few loops assembled in Acid Xpress and then imported into Unity as separate layers. Each layer is then turned on or off by the main controller at runtime allowing the soundtrack to change dynamically at key points during the game.
Overall the structure of this demo is much improved over the code in my previous demo, with very few uses of the GameObject.Find and GameObject.GetComponent methods to communicate between classes and much more focus on event broadcasting instead. Sure, it’s not the greatest looking game, but I’m pretty pleased with with how it plays, and while there are still a few camera bugs here and there, for the most part it works quite well.
Frameworks, plugins and open source code
There is now a wide range of excellent open source code available for use in your Unity projects and this demo makes use of a few of them including: the Detonator Framework, iTween and a customised version of this Unity C# Messenger system.
I’ve written about Detonator before, so I won’t go into much detail here, but I have to say it still impresses me every time I use it. There is no easier or more flexible way to create fantastic looking explosion effects in Unity and it scales really well to run on a whole range of computers too.
iTween is a fairly recent addition to the Unity community and is basically Unity’s answer to TweenLite. In short it is brilliant. It makes simple bits of animation an absolute breeze and more complex animation sequences easy too thanks to a built in delay system. Currently you can animate a GameObject’s position, scale, rotation, colour and volume to or from a given value with a single line of code. Hopefully future releases will add functionality for animating coloured lighting and other non-standard GameObjects and components but for now this has to be one of the most useful Unity scripts out there.
I’ve been looking for a good events system for Unity for a while now a recently came across the C# Messenger System on the Unify Wiki. After a bit of cutomisation I now have a system that is much more like the ActionScript event model, so rather than passing in an event as a string, you pass in an instance of an event class, helping to keep everything type checked by the compiler and avoid runtime errors, it also helps with code completion in Visual Studio which is a nice bonus. Hopefully I will get some time soon to tidy up the code for this and submit my version to the Unify Wiki.
A few things I’ve learned while making this demo
Making games in 3D can be difficult, even with Unity. The maths and overall structure of even a simple 3D game project is many times more complex than what is required for a 2D game. Unity really does a great job of giving you all the tools you need to make your game, but I found that you still need to understand some 3D maths if you want to try something that’s not covered by the standard camera and character controller scripts that come with the IDE. I found it really helpful to read up on the basics of general 3D games development covered by the official Unity tutorials as well as resources aimed at other platforms like XNA, Torque and even Flash.
If you want to loop background music in your game, don’t use MP3’s. Apparently MP3 meta data often adds a short amount of silence to the end of the file, meaning that when you play it back in Unity on a loop you will hear a short pop or click where the join is. There are many ways around this, but the most reliable seems to be to export your audio to WAV format and then let Unity compress it internally.
Hope this has been interesting reading, if you would like to try the demo, you can play it here.