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.
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:
- 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.)
- 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.