Frank Moricz


Web Portfolio and Art Gallery

Magic made with caffeine and a keyboard.

Megabite #1: Creating Your Own Game From Scratch (part 1)

I don't make any claim toward mastery of computer programming or game creation, but with the rise of indie games there seems to be much more of a question as to how one can create the game of their dreams - their very own indie game.  For this tutorial, I will be covering some basic concepts that were involved in creating this work in progress: G.I. R.type.

 

 

If the game isn't showing for you, go ahead and grab the Unity web player (if you arent on mobile) - you'll need it if you plan on playing any future titles here in the foreseeable future and you'll need the (free) Unity 3 full download if you plan on using any of my information to create a game of your own.

Why Unity?

Fair question.  With the array of different tools out there, theres a few reasons why I chose Unity to move forward with.

  • It's absolutely free for standalone PC/Mac games or Web games

  • Unity is capable of publishing to platforms such as Xbox, PS3, Android, and iPhone as well.

  • Unity utilizes javascript and C# in conjunction with many functions and shortcuts already included

  • There is a large community of helpful designers to answer questions - many of which are just a google away

  • Fully capable of both 3d and 2d games.  You can create a puzzle, platformer, FPS or Driving game to name a few, and do so without a great deal of prior programming knowledge.

To clarify a point early on: programming is not for everyone.  It is frustrating and tedious at times, and sometimes a misplaced character can keep an entire project from running properly.  That said, if you have a dream of getting your game out there and you see yourself making progress, the feeling can be exhilirating.

If you understand the warning here and wish to dig deeper, follow me onto page 2. :)

For simplicity, I'm going to start with a list of basic definitions that you will become familiar with through unity.

  • GameObject - Any and all objects within the game. They can be simple or extremely complex, visible or invisible, but if it exists in the game, it is a GameObject.

  • Transform - This pertains to the information that makes up a GameObject, whether it be its position, rotation, scale or one of many other possibilities.  Transforms are important to grasp as early as possible, and they will make the scripting of objects make much more sense.

  • Scene - This is the word used for a "level" in unity, though a scene can be your main menu, character select screen, high schores screen, or whatever you make it.  A project can be made of one scene or many - it all depends on your desire.

  • Prefab - This is a special game object with specific details.  Once you turn something into a prefab, duplication is made easy.  Though this is best described through working examples, simply understand that it is common practice to work with prefabs quite often, and they are a powerful method of saving yourself a lot of time and energy.

  • Variable - A variable can be many things: a number, a string of text, a GameObject, a Transform, etc.  Whatever it is comprised of, a variable is always a value of some sort that is stored in memory.

There will be many more strange words to come, but those can be covered as we go.  Remembering a glossary isn't the easiest way to learn anyhow.

Starting Up

Starting Unity for the first time will actually allow you to see and play with a built-in project called Angry Bots.  You can simply use the play button at the top of the window to start the game within unity and actually play it immediately.  Rather than attempting to describe the fairly complex demonstration, we will want to start a brand new project.  When prompted, check the boxes for all of the assets you are offered - this will copy them into your project's working folder for use later on.

Your new project screen should appear something like this:

This is actually the setup i've stuck with, though your preferences may vary.  You can fiddle with the tabs and layouts as you become familiar with what makes everything more comfortable for you.  At the top of the screen you have a "Game" tab as well as a "Scene" tab.  Your fun blue screen is the world as perceived through your main camera right now.  It's quite bleak.  Switch over to the "Scene" tab, and also click on your "Main Camera" in the list at the bottom-left portion of the screen - the Hierarchy.

On the next page, I'll explain some methods of manipulating your Scene and getting started.

Clicking directly on the scene will allow you to select GameObjects once you have some.  Right-clicking will let you rotate your view.  Like any 3d-modeling program, your middle mouse button and wheel will be needed for strafing and zooming as well.

Creating a World

Now things can get interesting.  In your Hierarchy tab you will have a "Create" menu.  The first thing we will do is create a cube.

Once created, you will ba able to use the colored arrows to position this cube however you like.  Chances are, your Main Camera is aimed elsewhere though, so you want to double-click the Main Camera within your Hierarchy window.

Without a point of reference, simply moving the camera to aim at the cube might be a difficult task.  However, we don't have to drag things around to move them.

In your Inspector tab you have all the tools you need to change the position and rotation of the camera.  Simply change the X, Y, and Z of the Position section to 0, 0, and 0, respectively.

Now, do the exact same thing with the cube and you will see that they are now directly on top of each other.  Using 0,0,0 as a point of reference can make your life a bit easier at this point.

Now, move your camera so that it shoots directly toward the cube.  Within the small preview window you should get an idea of how everything looks, but you can also feel free to use your play button at any time to see your game live.  Note: any changes made while your game is "playing" will be UNDONE once you stop the playback.  This is great for testing, but be sure to get in the habit of small playbacks.

In order to create an apple pie from scratch, you must first create the universe.

Many years ago, I attempted to create my very first game using my limited knowledge of Flash and Actionscript.  I started with the simplest concept I could think of at the time - a game of Blackjack.  It was only after I got myself started that I learned the truth: before you can write the actual game, you have to create the universe that the game will exist in.  In the case of my blackjack game, this meant designing and assigning values to each of the cards, explaining to the computer that there could never be duplicates of those cards within a deck, etc.  After a few were finished I saw the task of 52 simple cards become exponentially more daunting.

With Unity, some of these things are made much easier because the program already knows that what you are desgning will be a game.  Time will be saved from having to do things like adding a FPS type camera or designing what a car tire should do within a given scenario.  Fairly soon, you might take for granted some of this functionality - just trust me when I say how much time the program can save you in many cases.

At this point, your individual path will vary greatly.  Obviously a racing game is very different than a 2d puzzle game, but for our purposes we will focus on the functions and terms that ended up within G.I. R.Type because the code was designed with readers in mind.  Though my solutions may not always be the most elegant way to accomplish a task, I broke everything into segments so that it can be copied or referenced with greater ease.

Our First Script

Let's create our first hand-written script and create a custom function.  You may have noticed that in the menu for G.I. R.Type, the camera is basically spinning around the ship, and it appears that a spotlight shoots downward to light the ship as everything moves.  (To achieve this effect, I could have actually caused the ship to rotate while the camera stayed in place, or we can choose to rotate the camera while keeping the ship in a static position.)

Since the spinning camera is a great little piece of code that can be used in various places, let's take a look at that:

Confused?  Don't worry.  Lets break it down to make it more understandable.

First line:

  • var - This means "variable" - We're telling unity that we want to create a variable named "targetPosition".

  • Vector3 - we're working in 3d space, so a Vector3 is a set of 3d coordinates.  (remember, our cube is sitting at 0,0,0.)

Now, we've established that a variable is created.  Right now the variable is completely empty (or "null"), but it does exist when this script is run.

Second Section:

  • function Update() - Update() is an integrated feature of Unity, and anything inside will be done every single frame.  So, at 60 frames per second, whatever is inside this function will be done 60 times in a second.

  • transform.RotateAround() - Another great integrated feature.  We have to put something inside those parenthesis, but Unity knows what we want to do - we want to rotate the gameObject that this script attaches to around a central point.  In the above example, we are telling Unity that our target position will be the 3d coordinates that we assign to "targetPosition".

  • Vector3.up - We're telling unity that "up" is the direction we want to rotate toward.  the ".up" can be easily replaced with .down, .right, or .left to achieve different directions.  Choose whatever you like. :)

  • 10*Time.deltaTime - This translates to "10 times per second of REAL time".  Rather than really delve into this one, just know that the number 10 can be changed quite easily up or down to achieve different speeds.  Play around with it a bit until you're happy with the results.

On the next page, we'll talk about how to get your script created and attached to the camera.


Creating a Script Asset

In the Unity main menu, click on Assets, the Create, and then choose Javascript.  The script in the above example was pure javascript, so we'll use that as our first self-created asset.

Next to the Hierarchy window, you will see your blank script appear.  Name it whatever you like.

Drag it and drop it onto the Main Camera within the Hierarchy window.  This will attach the blank script to the camera.  When you click again on the Main Camera, you should see your script appear amongst the other features of the camera.

To actually add the code, double click on the new script within your asset window or where you see it listed in the inspector (the instance of the script name with the little page next to it.)  This will launch MonoDevelop, which is Unity's script-editor tool.

Again, here is the code you will want to add.

Make sure you account for ANY small error - it needs to be exact in order to function without problems.  Save it when finished and return to unity.  In the inspector window, you will now see something like this:

Tada! Though we could have told Unity exactly what coordinates we wanted the camera to spin around within the script, this makes things much easier to change on the fly.  In addition, you now have a script you can use differently for different situations.  To put it into perspective, you may not want the camera to spin around 0,0,0 in your second scene if you use the script.  This keeps you from having to write an entirely new file.

Hit your play button and enjoy the show.  You've just programmed your first code within unity, and have secured an asset that you can use within any new scene you choose to create.  From here, you are also able to move the location of the camera within the Scene window to change angles or distance.  No matter where you put that camera, it will rotate around that 0,0,0 point in space.

If you aren't too dizzy, uncheck the box next to the name of your script (the checkmark is pictured above).  The script is still there, but it is now disabled.  The spinning camera is cool, but we want to do something much more interesting now that we know how to create and attach scripts to game objects.

Let's Move the Cube

Create another brand-new Javascript Asset, but this time let's attach it to the cube GameObject.  Again, call it whatever you like.  I'm calling mine "CubeMover".

There are a few different ways to get something to move around in Unity (G.I. R.Type uses "force" applied to a "Rigidbody"), but for now we want to stick with the easily understood - the Transform.  As you can see within the unity inspector window, the "position" of the cube is currently 0,0,0.  These are the X,Y, and Z coordinates.  In the same terms explained above, the Vector3 of  transform.position would be 0,0,0.

Lets make that changeable.  In our new script, lets make it so that the UP arrow key increases the X value of the cube for as long as we hold it down.

Okay, so we have some new stuff to explain.  What the complete script basically says is:  'For every frame that the Input key "up" is held down, add "1" to whatever the value is for the Cube's X position'.

Write it and save it.  Your results will vary depending on where your camera is positioned, but if the cube moves when you press the up arrow, you've done it right. :)  Next, we'll add the other possible directions.

It's common to maintain that the Y direction is up/down and the X direction is forward/backward.  If you choose to go a different route, you totally can - it's your universe.  With this above code saved and added to the project, your cube can now move in four different directions.  This is perfect for many basic setups, but customization might be key later on.

There will be much more to come if I get some good feedback and questions from readers.  I am planning on working to get G.I. R.Type turned into a fully-functional and impressive game while explaining all of the components that currently exist within it.  After that, I will create a new project and begin again with new concepts (...Hmm, I may call the next one "Blaster Meister")

Please be sure to leave some comment love if you enjoyed the tutorial walkthrough so far.  Next week, I plan on talking about basic physics and colliders as well as interaction between GameObjects.  I wish I could have gone into more technical details this week, but I find that the best place to start is at the beginning, and the majority of readers may have little to no experience with Unity.

If you know anyone who would be interested in game design, please share with them as well.  Feedback and community will help to hone a more perfect product and assure that I am answering the right questions. :P

 

--Frank