Building games with Unity3D as a way of learning
In my last blog I wrote about an upcoming Knowabunga day at the tretton37 offices. The concept of the day was to give people the opportunity to pitch something they would like to spend the day learning more about and have others form up behind their topic.
The rules were simple, you get a couple of minutes to pitch your idea and in order for it to move beyond a pitch, at least one other person had to be willing to spend their day learning with you. If your pitch did not generate any interest you had to join one of the other teams.
We were all given the opportunity to post our pitches up front. A lot of interesting topics were brought up such as learning Clojure, using the Web Speech API, Web Components, exploring Roslyn and many many more.
However, the moment I saw it, I knew I wanted to form up behind a pitch that suggested we spend the day with Unity3D and build games! Turns out, so did others and we found ourselves in a group of 8 developers, all eager to get started hacking.
The above screenshot is from the game that I managed to write, from scratch in less than 6 hours without any real prior experience with Unity3D.
But let us rewind the tape and see what we did during the day.
The first order of the day for the group was to set a curriculum. We quickly came to the conclusion that the group were too big for us too all cooperate on a single project and decided that we would split into smaller teams.
In the end we broke up into 5 smaller groups, with me in a group of my own because I had my sights set on creating a side scrolling shoot 'em up game. I seemed like a daunting challenge to complete in less than a full work day, without having any prior experience, but I need a good challenge every now and then.
To get us started, the ones that had read up a bit started of by sharing what they knew about using from having done a bit of homework up front. Me and a co-worker had spent some time reading up on the basics and watching some of the learning material, so we shared our take on it with the group.
Priority one was to find some decent pre-made art assets that I could use. I could have opened an image editor and drawn something myself, but I figured that somewhere on the internet existed free art that would make my game look much cooler.
After searching for a while, I found a blog post on MSDN called "Free Space Art Assets to help you build your game". The graphics looked nice and the post said two magical things: free and no strings attached. Yay!
Entering the zone
After that, the rest is a bit of a blur to be honest. I more or less went into a hour coding frenzy - I got into the zone. I had a small mental check-list of things that I wanted the game to have by the end of the day.
The list was really modest and (more or less) looked like this
- A ship that can move across the screen
- A scrolling background (parallax if time permits)
- Enemies that fly across the screen
- Some sort of weapon that would let me blow the enemy scum to pieces
The basics of Unity3D are pretty straight forward. You add Game Objects to your scene. These game objects can have various Components added to them. Components are things like audio sources, sprite renderers, animators, colliders, rigidbody (makes your game object be affected by physics) and scripts (to name a few).
The scripts is what gives your game objects (and world) its behaviors and it is also the thing that made Unity3D so easy for me (and the other people in our group) to get up and running. The reason is that Unity3D built on top of Mono which enables you to write your game code using C# (or JavaScript or Boo). This meant that we did not have to learn a new language, while at the same time learn the concepts of Unity3D and game making in general.
When you attach a script to a game object, then the this reference in your script will refer to the game object that it is attached to. This means that you can create a general purpose script and reuse it across different game objects. You can attach multiple scripts to a game object, which makes them extremely powerful.
It helps if you think about the scripts as Behaviors (you actually inherit your code from a class called MonoBehavior) that you attach to your game objects. I can see myself build up a library of scripts that can be reused across multiple projects. Even the camera can have scripts attached to it, so you could create different kind of tracking behaviors (follow player, zoom out when you travel fast etc).
Wrapping it up
Before I knew it, it was time to wrap up and meet up with the other groups and demo what we had accomplished during the day.
By the end of the day my game contained
- An infinite scrolling background using a second camera, a quad and texture wrapping
- Three types of enemies, all flying across the screen in various sine curves
- Background music
- A ship that could fire missiles, complete with a pew-pew sound
- Missile explosion sound effect on enemy impact
- Enemy spawners
- Scripts that de-spawned missiles/enemies that fly of screen
- The start of a scoring system
Below are some screenshots of other games that were created (can you tell that many of them used the same art asset pack? Free stuff is awesome, he-he) during the day.
I do not have all the details on all these games, but I can tell you that the last one is a 2-4 player game that uses wireless Xbox 360 controllers to steer your skateboarder. The objective is to try and knock everyone else of the platforms.
The aftermath
Although I did not have enough time to add the scoring, but I revisited the code over the weekend and added it in, along with particle system based explosions when an enemy was hit. I also added a pause option with a single line of code (again, wrapped it up in a Pause-script that can be re-used).
It is still undecided if I am going to publish the project on GitHub or not. I am all for open-source, but I need to make sure that the audio assets, that I grabbed of random sites on the internet, does not violate any licenses. The biggest challenge would be to find the sites where I found them.
However, I will most definitely keep evolving my knowledge with Unity3D, because game development is something I have always (and periodically have done using other platforms and tools) wanted to pick up and be more active with. It is a very rewarding and creative process that goes hand in hand with my passion for programming.
The free version of Unity3D is great and it works both on Windows and OSX. Unity3D also lets you target a wide range of platforms such as Windows, OSX, Linux, PS3 (PS4 on the way), XBox, Wii-U, Windows Phone, Windows Store Apps, Blackberry 10, iOS, Android and the web with the help of WebGL.