Unity is a multiplatform game development tool, designed from the start to ease creation. A fully integrated professional application, Unity just happens to contain the most powerful engine this side of a million dollars.

Report article RSS Feed Unity Tutoral Part 1: In the beginning..

In this tutorial i will explain how to start developing a game using the Unity3D engine and its toolset.

Posted by Arxae on Mar 9th, 2010
Intermediate Other.

Welcome, have a seat :D

In this tutorial series we will develop a game from scratch. Don't expect anything fancy, but you will get a good idea how easy and fun it is to develop games using Unity.
You will get to know what GameObjects and Components are, what they have in common and scripting, since we wouldn't have any gameplay otherwise :D

I will be vague on some details on purpose and not much screenshots. Why? you ask. Well i think if my explanation isn't enough and you don't look a bit on your own, then you are probably not cut out for this kind of job :D
So i am promoting a bit of self study here :)

In this first part we will go over the following:

  • Where to get Unity
  • The interface
  • Controls of the editor
  • Adding GameObjects
  • Getting a basic player character going

Lets begin shall we? :D


You can download the free indie version of Unity right here. It works on Windows and Mac.
I am using the pro version myself but the workflow is pretty much the same. The only difference with the pro version are some tools (Game profiler), some graphic options (Shadows) and some other things (iPhone support).
But you can just get the indie version and play around with that first :D

So after installing unity and activating the indie license, start it up.
You will be greeted with the Project Wizard. Select the "Create New Project" tab if it isn't already selected.
Point it to a location and deselect all the import packages, we are starting from scratch remember. We can import a package (or some files out of it) later if we want to (and we will for 1 thing)
Press create and you will see the editor


I will give a brief explanation of the important buttons and panels:

1. Tool buttons: From left to right: Pan, Move, Rotate, Scale.
You can manipulate objects with these tools. They are pretty much self explanatory.

2. The Project/Hierarchy/Inspector panels:

  • Project Panel: This panel will contain all your assets. You prepare them here and them arrange them in a scene. Its always a good idea to work organized and categorize everything in folders. You also have a search bar here (it updates in real time)
  • Hierarchy Panel: This panel contains all the scene elements and parent/child groupings. So in other words: Everything that appears here will be seen by the player at some point.
  • Inspector Panel: In this panel you can modify the properties of objects and add components to them. You can even change values while playing the game (note that they get reset to what they where before you started playing)
  • Expect to visit this panel pretty often (all three of these actually.

3. The viewport buttons: You can set some viewport options here, like wireframe rendering, no lighting, skybox toggle etc...
You can also find the Scene editor and Game window tab (pro tip: the tabs switch automatically when you toggle the play button)

4. Game control: From left to right: Play, Pause, Next step.
You will most likely only use the play button.

You can move and resize pretty much all of the windows. So setup until you are comfortable with them (I will be using the default positions)

Moving around in the editor is easy as pie. While holding right click, you can use the standard WASD/QZSD configuration as in most first person shooters.
If you, like me, have a different setup then WASD you can change the controls in the preferences (Edit -> Preferences -> Keys and then look at the one prefixed with View/)

You can pan around by holding down ALT and the Mousewheel button.
Orbitting around the center point can be done by holding down ALT and the right mouse button.

The final key i will explain will be the focus key. By pressing 'F' you will focus on the currently selected object. Unity will place it in the center of the screen and at a distance so you can see the object fully.

Thats about it for the crash course in Unity control. Now lets start developing :D


First, lets make a floor to test all our stuff.
Go to GameObject -> Create Other -> Cube and you should see a cube pop up in the middle of your screen.
But, Unity has the bad habit of placing objects in the center of your screen, x amount of distance away from your editor camera = in weird places.
So use the inspector and place the cube at 0, 0, 0 (X, Y, Z)
Now, we need a floor, not a cube. So scale it a bit so you get a bug plane (I scaled mine to 50, 0.2, 50)
And yes i am aware we could have used a plane, but it doesn't matter.
You should also rename the plane so you can identify it better. Select the cube and press F2 (Or you can change the name in the Inspector panel) to rename. I will call mine _Floor.

Now we have our basic environment we can use to test our scripts and such.
If you press the play button you will be bored: A blue background with some grey..and it's dark.
Let's add a light: GameObject -> Create Other -> Point Light.
Remember the weird position? It happened again, move it to 0, 0, 0 and then place it in a good spot.
I also modified the Intensity to .5 and the range to 100

If you press play now you will still be bored, but you can see more. Let's start on our character.

I should point out first that Unity is NOT A MODELER. Even tough it includes some primitives you should try to use them as less as you can. They are good for prototyping your game. But that is about it. If you want a good character, then model one and import it.
Now lets ignore the rule for once for the sake of keeping it simple (but only this time!)
Let's make a sphere (you will see later why a sphere ;)). You should know where it is by now.
Rename the sphere to something better. I will use _Player
Standard procedure for new objects (0, 0, 0) and move it up a bit. Boring Sphere. Lets make a script :D

Make a Scripts folder in the project panel (You don't have to but its recommended to stay organized)
Next you should create a new JavaScript (JS). We are going to use JavaScript in preference of C# here since it works better in Unity.
Move the script to the Scripts folder by dragging and dropping if its not there already and rename it to a better name (Like _PlayerScript).
Quick note, i use _FileName to show that those file have a higher priority then some others. Like now, this script controls the player, so it is a very important script or in case with the _Player and _Floor, i named them like that so they are on top of the Hierarchy list.
Doubleclick on the file and let's script :D


You should start out with the following:

javascript code:
function Update () {
}

The Update() function gets called every frame. We will be placing our control code here. But lets add some variables first.
Add the following to the very top of the file:

javascript code:
// Movement Variables
var speed = 3.0;
var rotateSpeed = 3.0;

If any programmer sees this. Yes i am not using Public, Private etc.. since they have another meaning in Unity. You will see this later.
Now modify the Update function so it looks like this (I have commented on every line to explain the code)

javascript code:
function Update()
{
  // Rotate around the y-axis
  // Get the object this script is attached to
  // A CharacterController is a object with simplified physics for use as a character
  var controller : CharacterController = GetComponent(CharacterController);
  // The actual rotationcode.
  // Rotate takes 3 parameters: X, Y, Z
  // GetAxis gets if a axis is pressed. A axis in Unity is a key. You can attach keys to axises so that a player can reconfigure them.
  // It is recommended to use axises, otherwise you will need to take into account that a player might want to change it keys. Using this method, unity handles this for you.
  // Horizontal axis in this case = Left/Right arrow keys
  transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
 
  // Move forward/backward
  // Gets the forward vector and place it in the foward var (_forward to distinguish it from Vector3.Forward)
  var _forward = transform.TransformDirection(Vector3.forward);
  // Gets the current speed
  var curSpeed = speed * Input.GetAxis("Vertical");
  // Makes the controller move using our speed
  controller.SimpleMove(_Forward * curSpeed);
}
// Makes sure you have a CharacterController Component on the object you attach this script to
@script RequireComponent(CharacterController)

Note the difference in place for the variables. The ones we defined on top (speed and rotateSpeed) are more like parameters. You will see this when we attach our script.
How to attach the script to our player? Simple! drag the script from the Project panel to the _Player object in the Hierarchy window. You can also drop it in the inspector panel when you have selected _Player.
You will see that a Character Controller Component gets added, this happened due to "@script RequireComponent(CharacterController) " making this component required
Try to delete the Character Controller if you like (Click on the little gear on the top right of the component box) but you will see you can't since it is required.

This is also your first component. You already knew about GameObjects.
GameObjects live in a scene but Components live in a GameObject. They give it special properties.
Browse trough the Component menu, there are allot of components that make up our GameObjects. We actually added 2 already :D -> A character controller (came because of the requirement of our _PlayerScript) and a Script.
There where already 3 components attached to our sphere because they needed to be.
A Sphere (Mesh Filter) that passes on your mesh from the project to the mesh render.
The mesh renderer itself that renders the mesh in the engine
And a Sphere collider that makes sure you can bounce into stuff and not phase trough them like a ghost. You can see that one on your object when you have it selected (the green lines)
Most primitives have a variation of this (mostly a different collider)

I told you about the variables. You can see them here, take a look at the Script component. See the Speed and Rotate Speed? We defined those.
You can use this to tweak your object or just to have some variation without scripting it all.
For example: you have 2 monsters, a older one and a younger one. They are both the same except for speed, strength and model
You could make 2 scripts for them OR you could just place 2 monster objects and give them different models and stats. One script, endless variations ;) (well almost)

If you press the play button now you should be able to use the arrow keys to use your character around (watch out for the edge!). But what if we move past the camera? well we could fix that in a number of ways depending on the type of game. All of them involve attaching the camera to something.
But lets make a Third person type of game. We COULD script a camera follow function but we are not gonna. Unity comes with a good script for this, so lets use it :D This way i can also show you how to import stuff.

Right click in your Project window and select "Import Package". You can find all the standard packages in /Editor/Standard packages. Select the Standard Assets package and press open
After its done decompressing you get a list of its contents. We only need one script so press the "None" button at the bottom and look for the SmoothFollow script (Standard Assets/Camera Scripts)
Select it and press the import button. You should see a Standard Assets folder with a Camera Scripts folder inside.
I reorganized it by placing the SmoothFollow script in my scripts folder and get rid of the Standard Assets folder (so should you, stay organized!!)
The script is pretty much Plug - Configure - Play. We just need to attach it to a GameObject.
Now, the script works by letting the object it is attached to follow something else, so it makes sense to attach it to our camera. Rename the Main Camera object to something along the lines of _PlayerCamera and attach the SmoothFollow script to it just like before
There is one thing that is required for it to work -> something to follow.
Look at the inspector view (select the camera first) and you will see Target with None (Transform) behind it.
A transform is just a word to describe the position, rotation, scale, etc.. of a object.
There are 2 ways we can attach our players to the script:

  1. Drag the _Player object to the space and release the mouse button
  2. Click on the little arrow next to None (Transform) and select _Player

The main advantage of method #2 is that it only lists the applicable type of objects

If you press play now, the camera should follow our sphere :D The only thing left is to tweak some settings. Don't leave play state for it, just select our camera and change the settings (note that they are not saved in this way, but its great for tweaking, so remember the values)
My settings where:
Distance: 7
Height: 2
Both Dampings: 6
You can also up the speed of the _Player object a bit if you like, i have set mine to 10

You now have a fully function..walking sphere...
But this is it for part 1. In the next part we will expand a bit by allowing our character to shoot and making it look a bit more like a actual character. The reason why spheres are being used will be revealed to :D
Don't forget to do File > save project and File > Save scene :) just give the scene a name, doesn't really matter at this point since this is only a test scene.

I have uploaded the current project file to here on moddb so if you are stuck you can download this and have a look. Just import the assets (Copy them to your own assets folder using your file manager) and open the scene in the scene folder
If they don't appear, Right click the project panel and select Refresh
You can download the assets here

If you have any questions you may send me a PM (comments are fine to, but a PM guarantees you a response from me :))
Just experiment a bit with what you have learned and keep on the look out for part 2 :)

Post comment Comments
turbo91
turbo91 Jul 10 2010, 12:10am says:

AWSOME!

+1 vote     reply to comment
3SidedSquare
3SidedSquare Aug 20 2010, 2:43pm says:

I seem to have a small problem, it seems that Unity can't identify _forward.

Assets/NewBehaviourScript.js(22,25): BCE0005: Unknown identifier: '_Forward'.

+1 vote     reply to comment
SirionDawn
SirionDawn Aug 24 2010, 5:11pm replied:

(_forward to distinguish it from Vector3.Forward)

I was wondering about that myself, cause it didn't work for me either but I eventually replaced both with "Forward" (without the quotes) and it worked. Not sure if this is a correct permanent solution but it looks ok for now.

+1 vote     reply to comment
Arxae Author
Arxae Oct 7 2010, 7:14pm replied:

a bit late :p
but JavaScript is case sensitive
you have written _Forward
but it should have been _forward
_Forward
_forward
stupid mistake xD i make em too from time to time :p

+1 vote   reply to comment
SirionDawn
SirionDawn Oct 14 2010, 1:32am replied:

Oops, we should have picked up on that ourselves. I thought it had something to do with the name of the variable starting with an underscore so I renamed it (+1 for the beginner's mind xD).

Thanks for the reply! Any info on when can we expect part 2? ^^

+1 vote     reply to comment
Shadolon
Shadolon Jul 10 2013, 1:18pm replied:

oooooh

+1 vote     reply to comment
Shadolon
Shadolon Jul 10 2013, 1:17pm replied:

yeah that happened for me to

+1 vote     reply to comment
chust
chust Feb 2 2011, 6:54am says:

Woaah, this helped me so much!
Thanks!
Will you make a part 2? <3

+1 vote     reply to comment
AnacondaTrg
AnacondaTrg Jul 2 2011, 2:43am says:

this helpfull

+1 vote     reply to comment
Guest
Guest Feb 26 2013, 9:30am says:

This comment is currently awaiting admin approval, join now to view.

Guest
Guest Dec 8 2013, 9:45pm says:

This comment is currently awaiting admin approval, join now to view.

Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Contact
Send Message
Official Page
Unity3d.com
Licence
Commercial
Release Date
Released May 30, 2005
Engine Watch
Track this engine
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Engines
Unity
Unity Commercial Released May 30, 2005
Related Groups
Unity Technologies
Unity Technologies Developer