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