Unity 3D – Asteroids Style Demo

Standard

Unity Astrospace Demo - Click to Play

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
I wrote the scripts for this project in C# rather than the standard Unity JavaScript. I chose to try and learn C# after reading this post on EverydayFlash which had recommended it. It took a little getting used to but it was definitely worth learning, especially now that Visual Studio C# syncs with your Unity projects, as you get the bonus of using one of the best code editors out there to write your game code as well.

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.

Click to Play Unity Astrospace Demo

Advertisements

10 thoughts on “Unity 3D – Asteroids Style Demo

  1. cjcat2266

    Wow. For a first project on Unity3D, this is pretty awesome! I already learned c#, but cannot find a good tutorial source for Unity3D. Any suggestion?

    • Matt

      Thanks CJ! I guess the ready made graphics and effects do add a lot to the demo, I did have an earlier version that just used my own graphics, but it just didn’t look anywhere near as good… For tutorials, well there’s a few good links in my previous post about Unity, but the best place to start if you already know C# is probably the tutorial projects on the official Unity website or Will Goldstone’s Unity Game Development book, both do an excellent job at explaining the user interface and showing you how to structure a project in Unity.

      • jhuculak

        I’ve done a couple of tutorials that may interest you on igamemaker.com including one that is about asteroids (Makeroids). I did all the artwork and I am in the process of doing how-to videos. The project file for the asteroids one is $2 but it might help you learn some a few things.

      • Matt

        Thanks for the info, those tutorials look really interesting. Have just played the Makeroids game on your blog as well and gotta say it’s pretty impressive, especially as you did all the graphics and sound yourself too! Would definitely be interested in seeing more Unity tutorials like that in the future 🙂

  2. hamedaf

    Hey Matt,

    you know how hard it is to find your email or your contact details? ha ha

    well, I am really interested in your demo since I am learning to port a game I did using Flixel to Unity, and I was hoping you could help me out.

    • Matt

      Hi Hamedaf, that sounds like a cool project, though my knowledge of 2D Unity development is still pretty limited so I’m not sure how much I could help… However, here’s a couple of links that I think you might find useful if you’re trying to port a 2D sprite based game over to Unity…

      http://www.rocket5studios.com/tutorials/creating-2d-games-with-unity3d-part-1/
      This is the first part in a great new series of tutorials about making 2D games in Unity. There are 2 parts so far, with more to follow in the future, and from what I’ve read so far I think they provide an excellent overview of how to setup a proper 2D game in Unity using a 3rd party plugin called Sprite Manager. The rocket5 blog says that part 3 should be online soon and will cover making a simple 2D platform game, which sounds really great too.

      http://unity3d.com/support/resources/tutorials/2d-gameplay-tutorial
      This is the official Unity demo project for making 2D games. I found that the project files and related tutorial were very helpful when I was making my asteroids demo, so it’s definitely worth checking out as well.

      Hope this info helps 🙂

      • hamedaf

        most importantly, i am wondering about how you did the thrust in the ship, as well as the particle effects for the after burner behind the ship.

      • Matt

        Hey Hamedaf, sorry for not replying sooner, have been very busy here. Hope that work on your game is going well, from the comments I saw on that other post you linked to, it looks like you have now found some good solutions to those ship rotation and movement problems.

        To be honest, I’m still learning a lot of about 2D and 3D math myself, which is why I have yet to post any Unity code, but hopefully I will write some actual Unity tutorials in the future… Thanks for posting the link to that unity3dtutorial site as well, there are some really helpful articles on there.

        In terms of the particle effects I used, that really was just a case of attaching an instance of Unity’s built in Particle System component to the back of the ship and then adjusting the values in the Unity editor until it looked good. There’s a lot of variables to consider, but the official Unity documentation does a great job of explaining what all the different settings do – http://unity3d.com/support/documentation/Manual/Particle%20Systems.html

        Good luck with the rest of your game 🙂

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s