Unity 101: Learn Unity Game Development in 10 minutes
page
This is Unity 101 in 10 minutes and you are about to create your first video game!
- Create your first playable game scene
- Download, install and open Unity. (Download and install time doesn't count against the 10 minutes!)
- Create a new project by clicking File Menu / New Project (You can play with Angry Bots later!)
- Choose to import just Character Controller to save time, you can import more later from Assets Menu / Import.
- (Or choose everything to give yourself more to play with, but this blows the 10 minute thing!)
- In Hierarchy's top left corner click Create and click Cube.
- Click the fourth tool in the top right or hit 'R'.
- Resize the cube wide and flat ( scale 50,1,50 ) as a ground by dragging the colored boxes on its sides.
- Click its name in the top right Inspfector. Name it "ground".
- Drag a character controller from the Project view to the Scene view or Hierarchy.
- Position it above your ground cube.
- Test It!
- Click PLAY button top center.
- Your game is now running! Walk around with WASD and look around with the mouse.
- Click PLAY button again to stop.
- Remember: NEVER edit the game while it is playing or you'll lose changes, always stop it first.
- Save your Scene which is like your first level of your game and name it "level1".
- You can make it awesome in the next steps!
- Decorate your scene!
- Add point lights to make it look cool and more cubes to jump around on.
- Next we'll learn how to add a script to stop yourself from falling infinitely off the edge.
- Create a lava cube to avoid falling infinitely
- Select the "ground" cube.
- Edit Menu / Duplicate it.
- Move the duplicate down.
- Rename the duplicate "lava".
- Resize it to make it wider (100,1,100 scale) so it will catch you if you fall.
- Hit PLAY and try falling off the edge. Notice it catches you but you can walk further and fall off the edge still.
- Hit PLAY again to stop it.
- Click the "lava" cube to select it.
- In the Inspector, look for the Collider component.
- Check the "Is Trigger" checkbox.
- Hit PLAY and try falling off the edge. Notice it you now fall right through the lava cube without stopping.
- Hit PLAY again to stop the game.
- Create your first script code to avoid falling infinitely
- In the Project pane, click Create / Javascript.
- Rename the javascript "Fall". Click it once to make the name changeable.
- Drag the Fall script to the "lava" cube in the Scene, the Hierarchy or the bottom of its Inspector to attach it.
- Look in the inspector to confirm it is attached to the "lava" cube.
- Edit the script to handle an OnTriggerEnter event:
- Double-click the "Fall" javascript to edit it.
- Wait for MonoDevelop to load up.
- Double-click the "Fall" javascript again if it is not already open to edit it.
- Select all the "function Update" text and erase everything.
- Type just "trigger". We are going to find out how triggers work by searching the API Reference.
- Make sure the cursor is on "trigger" or select the whole word.
- Click the Help Menu and choose Unity API Reference to look it up.
- You can do this faster by pressing Command-' (Mac) or Control-' (Windows).
- Look at the results that come up for something that looks related to triggering an event when a collider is touched.
- Click Result 2: OnTriggerEnter.
- Select and copy the example code.
- Switch back to MonoDevelop.
- Erase the word trigger.
- Paste the example code into Fall.js.
- Delete the whole "Destroy" line.
- Your code should look like this now:
function OnTriggerEnter( other : Collider ) {
}
- Change the Destroy line to reload the current level instead.
- Select and delete the GameObject.Destroy line.
- Since we want to reload the same level, type "loadlevel" Your code should look like this:
function OnTriggerEnter( other : Collider ) {
loadlevel
} - Click "loadlevel" and click Help Menu / Unity API Reference to look it up.
- The first result is the one we want! Click Application.LoadLevel
- Copy the example code.
- Switch to MonoDevelop.
- Paste the example code on a blank line between the "function OnTriggerEnter {" and "}".
Your code should look like this:
function OnTriggerEnter( other : Collider ) {
Application.LoadLevel("Highscore");
} - Change Application.LoadLevel("Highscore"); to "Application.LoadLevel("level1");"
Your code should look like this:
function OnTriggerEnter( other : Collider ) {
Application.LoadLevel("level1");
} - Save your code.
- Switch to Unity.
- Save your scene and name the scene file "level1".
- If you get an error about Build Settings, make sure to add it by going to File/Build Settings/Add Current.
- Hit PLAY to test it.
- When you jump off the edge and hit the lava, do you get teleported back to the top?
- If so, you are now a game developer, congratulations!
- Expand your game level
- Make 10 cube platforms and position them with gaps so that you have to jump between the cubes.
- Arrange the cube gaps at increasing distances and difficulties until the last couple jumps are quite difficult but not impossible for you.
- Make sure that you always get teleported back to the start if you fall from anywhere in the level.
- Make a portal that teleports between two levels.
- Make a vertical wide narrow cube that looks like a door. (2.0, 3.0, 0.5 scale)
- Set it to IsTrigger = true (checked).
- Duplicate the Fall.js script.
- Rename the duplicate script Portal.js.
- Attach Portal.js to the door by dragging it onto the object or its name in the Hierarchy list part of the window.
- Double click Portal.js.
- Add a new line at the top that reads "var levelToLoad : String = "level2";
- Replace "level1" with levelToLoad.
Your code should look like this:
var levelToLoad : String = "level2";
function OnTriggerEnter( other : Collider ) {
Application.LoadLevel( levelToLoad );
} - Make level2 so your portal has somewhere to go:
- File Menu / New Scene.
- Save the scene as "level2".
- Repeat steps 1-9 to make yourself a new level with a new or duplicate level1 as a starting template and rename the duplicate "level2"
- Make sure the name of the second level matches the name referred to in the portal.js script
- Load the first level again
- Play the first door
- If it takes you to the first level again, congrats!
Make Objectives: Coins, components that talk to World
Games are made of Components that are attached to GameObjects. For two components to talk to each other, they have to find the game object the other component lives on and GetComponent the component that lives there to send that component a message.
Make Coin scripts that send messages to World script which displays total coins:
- Make a cube.
- Coin: In Inspector, name it "Coin"
- Make its Collider component Trigger checkbox enabled
- In Project, Create a Javascript
- Name the Javascript "Coin"
- Open Coin.js in MonoDevelop.
The code should look like this:
var world : World;
function Start() {
world = GameObject.Find("World").GetComponent("World");
}
function OnTriggerEnter( other : Collider ) {
world.SendMessage("AddCoin");
renderer.enabled = false;
collider.enabled = false;
if ( audio ) {
audio.Play();
}
} - Drag Coin.js onto your Coin game object in the scene.
- World: In GameObject menu, create an Empty GameObject.
- In Inspector, name it "World".
- In Project, Create a Javascript
- Name the Javascript "World"
- Open World.js in MonoDevelop.
The code should look like this:
var coins : int = 0;
function AddCoin () {
coins++;
}
function OnGUI() {
GUI.Label( Rect( 0, 0, 100, 100 ), "COINS:"+coins);
} - Drag World.js onto the World game object in the Hierarchy.
- AUDIO: Component Menu / Audio / Audio Source.
- Google search for a suitable coin sound like this one(Right click, save to disk) to download and drag it into the Project folder box to add it to your Assets.
- In Inspector, drag the sound to the Audio Source component's Audio Clip field.
- Uncheck Play On Awake.
- PREFAB: In Project, create a Prefab named Coin.
- Drag the Coin game object onto the gray Prefab. Its name turns blue in the Hierarchy list and its cube icon turns blue in the Project.
- Duplicate the coin in the scene and place lots of coins around.
- Hit PLAY.
- GET ALL THE COINS!
- CHALLENGE: Keep track of the total coins to be found and display a different message once the player finds all the coins in the level!
Next Challenges:
- GameObjects
- Make cubes that trigger things
- Components
- GameObject.Find( "objectName" ).GetComponent( "className" )
- Transforms
- Making something move


