Unity 3D – Space Runner Game Demo

Standard

Space Runner Unity Demo - Click To Play

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.

Space Runner - Click To 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.

Space Runner - Click To Play

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.

Advertisements

8 thoughts on “Unity 3D – Space Runner Game Demo

    • Matt

      Thanks, I haven’t put the code online for my Unity demos yet as I’m still learning it as well, so some of the code needs a bit more work. If you’re interested in learning Unity then I’d definitely recommend the sites below as the best places to start.

      http://unity3d.com/support/resources/
      The official Unity website has loads of great resources, there are lots of really well written tutorials for different kinds of games like FPS, 2D and 3D platformers and even a 3D racing game.

      http://www.everydayflash.com/
      This is a great blog about 3D programming for both Unity and Flash developers. It also has lots of info about learning C# for Unity which I found very helpful.

      Hope this info helps.

    • Matt

      Thanks James, glad you liked the demo! The star field is actually made using the standard Particle System Component in Unity plus a few extra lines of code, will try and explain the basics of it below.

      First you need to add a new Particle System Component to your scene, then edit the settings of that particle system until you have got something that looks fairly similar to the star field that you want to use in your game. Then you need to turn off the particle system’s emitter, this will stop it animating automatically. You then need to create a new Script Component and add the following three lines inside the Start() function at the top of that script.

      particleEmitter.emit = true;
      particleEmitter.Simulate(10);
      particleEmitter.enabled = false;

      This code will run when your game starts and will make the particle system animation fast-forward 10 seconds and then pause again. Now all you need to do is save the script and add it to your star field, then run your project in Unity.

      In my demo, I’ve saved the star field as a prefab, and then I’m adding two copies of it to the scene at runtime, about 1000 units apart. Then as each star field disappears behind the camera, I move it forward in space by about 2500 units so that the stars just loop round the camera (if you’d like to see a simple example of this in Flash, check out the code from my stardust x-wing demo).

      Hopefully this info helps, here’s a couple of links that you might also find useful.

      UnityAnswers – Particle system starting states
      http://answers.unity3d.com/questions/1932/starting-state-for-a-particle-system

      Unity – Particle system component docs
      http://unity3d.com/support/documentation/Components/comp-ParticlesGroup.html

  1. Hi Matt,

    I don’t know if you hare still updating this page or not.

    I want to create a game based loosely on the space runner demo. I have encountered various issues with this.

    Any chance of getting you project\source for me to work with? It is for a free game to be played on Facebook.

    Thanx,

    Amichai

    • Matt

      Hi Amichai, thanks for your interest in the space runner demo, however I haven’t posted any of the code from my Unity demos at the moment, mainly because I’m still learning a lot about Unity and C# myself, so I’m not that sure how good the code is, or how useful it would be…

      However, here’s a few links that you might find helpful, they are to some other tutorials and code examples that cover similar 3D spaceship games…

      http://infiniteunity3d.com/save-the-princess-open-source-game-made-with-unity/
      This game looks like a very good example of a starfox style game made in Unity. The game is open source and has controls setup for use with keyboards and control pads.

      http://unity3d.com/support/resources/tutorials/iphone-multiplayer-tutorial
      Unity iPhone also comes with a very well written spaceship demo, which is actually available to download as a standalone tutorial from their website. Although the controls are setup with motion control in mind, I’ve seen that some people have had a go at converting them to work on a desktop PC with keyboard / mouse support as well.

      Hope this info helps, good luck with 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