This demo is my first project in Unity, and is my take on the Atari classic Asteroids. There’s a lot to learn when you first start out using Unity, so something simple, well known and in 2D seemed a good place to begin. If you’d like to play the demo, please click the image above.
Setting up a 2.5D game in Unity
While Unity is mainly a 3D game engine, using it for 2D games is just a case of adding a Configurable Joint component to each GameObject and Prefab that you want to restrict to only 2 axis and setting the corresponding X,Y or ZMotion properties to locked. This will stop those specific objects from being affected by the PhysX engine on the chosen axis while also leaving other game elements (including the Main Camera) free to move wherever you like in order to add depth the scene.
The best example of this I’ve seen so far is Paper Moon by Infinite Ammo, the “2D with depth” style of that game kind of reminds me of early 32bit era 2.5D platform games like Bug! and Clockwork Knight as well as more modern stuff like LittleBigPlanet and Viewtiful Joe. Overall, the effect is similar to what can be achieved using Box2D with Papervision3D in Flash, but using Unity gives you a bit more freedom to pile on the graphical effects without worrying so much about the frame rate.
Making the demo
While the code was all written from scratch by me, the graphics and sounds effects are mainly from asset packs and tutorials available from the Unity Technologies website, while the music was from an old Computer Arts CD. In addition to all this I used the excellent Detonator Framework by Ben Throop to create all the pretty explosions you see in the demo. Just like Flash, there are loads of open source frameworks and code libraries out there for Unity, and lots of great tutorials to help you get started making games.
Over all I’m quite pleased with how the demo turned out and very impressed with the Unity editor and how easy it is to use. I guess the one thing that can make Unity quite difficult is the need for some knowledge of a 3D modeling package, without that you kind of have to rely on ready made models as I did for this demo, this can be a bit limiting at times and does add an extra few degrees to Unity’s learning curve. Might be time to start learning Blender…
A few things I’ve learned about Unity while making this demo:
- Well structured code is more important than ever. At times, coding in Unity can seem a bit like Flash back in the days of AS1, you have the freedom to attach your code to almost any in game object and this can make it very easy to get in a muddle.
- Public properties are great. Making a property public in a Unity script means that you can then change its value through the editor’s inspector menu. This is useful as you can alter these values at any time without editing the script, even while the game is running in preview mode, this makes it very easy to tweak the gameplay until it’s just right.
- Preview mode doesn’t save your property settings. Following on from the previous point, be aware that when you close preview mode all those changes you just made to those public properties get reset. This can be a nightmare if you didn’t know in advance, or simply just forgot about it, thankfully there is a solution.
- Any script can access any other part of the current game scene. Using GameObject.Find() and GetComponent() methods can at times be Unity’s equivalent to using parent and root keywords in Flash, with all the problems they can cause. You can end up writing very specific code when a good Event Messaging and Listener system is probably a much better option.
- The Unity GUI system is rather quirky. Perhaps I’m just too used to Flash and Creative Suite in general, but the way Unity handles User Interface programming and design really does take some getting used too.
- If you’re from a Flash background, you will miss tweening. There are a few tweening libraries out there for Unity, the most advanced seems to be AniMate which is written in Boo, but I couldn’t find anything that seemed to work well with C# just yet. One thing Unity really needs is a port of TweenMax for Flash.
- For all the similarities, Unity isn’t Flash in 3D. An obvious one I know, especially given everything I’ve written above, but there’s some things that Flash does that it’s very easy to take for granted. It took me ages to work out how to fade the alpha transparency of a 3D GameObject without a tweening library or a simple alpha property. Turns out fading a 2D texture was fairly simple, but a GameObject containing several 3D meshes requires you to target the alpha of the main colour of every texture within it, cue some boolean flags and a nested for loop just to make an object fade out.
Anyway, that is probably enough talk for now, if you haven’t played the demo yet please check it out at the link below.