Gamebridge/Trigger Tutorial
Learning to make games with Unity is easy with the right tutorials and resources.
- Coding Jar Tutorials
- Self-paced tutorials with guided voice and automatically advancing steps.
- Download them and give them a go.
- Unity 101 Tutorials
- Fall triggers
- Coins
- Unity/Learn has loads of great tutorials.
- Scripting
Unity 101: Fall Triggers
This is Unity 101 in 30 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");"
- 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 level2 so your portal has somewhere to go:
Code Hero 102: Coins
Once you've made your first level have obstacles to avoid falling forever, it's time to add some objectives for your players to seek out.
- Create a Coin.js script
- Create a World.js script
- Make Coin.js a trigger that sends an AddCoin message to World.js
- Make World.js increment a coin integer variable and display it OnGUI.
This is Unity 101 in 10 minutes and you are about to create your first video game!
- Download, install and open Unity.
- Create your first project and your first playable game scene.
- Create a new project by clicking File Menu / New Project (You can play with Angry Bots later!)
- Click the Character Controller checkbox to import it. (You can import more stuff later from Assets Menu / Import.)
- In Hierarchy's top left corner click Create and click Cube. (You can also click Game Object menu/Create Other/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 "Teleport". Click it once to make the name changeable.
- Drag the Teleport 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 "Teleport" javascript to edit it.
- Wait for MonoDevelop to load up.
- Double-click the "Teleport" 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 Teleport.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");"
- 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!
Here's what your first level should look like:
File:Unity101LavaTriggerScript.jpg
Congratulations! You're ready for the next tutorial:
Part 2: Coin & World Components
More Ways To Make Your First Level Fun
Now that you have a basic platforming game level that you can jump around in and try not to fall off the edge, it is time to make your level exciting and fun.
- Make your lava red
- In Project panel, click Assets.
- Click Create and click Material.
- Type the name "LavaMaterial" and hit return.
- In the Inspector panel, there's a white box to the right of Main Color. Click it.
- In the Color panel that pops up, pick a reddish color and close the Color panel.
- In Project panel, drag the LavaMaterial into the Hierarchy panel to drop it on the Lava object.
- 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 Teleport.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.
- File menu / 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.
- Add each of the levels in your game to the Build Settings list by clicking File Menu / Build Settings... and clicking Add Current after loading each one.
- Load the first level again.
- Play and walk into the first door.
- If it takes you to the first level again, congrats!
- Make the door white:
- In Project Panel, click Create / Material.
- I
- Make level2 so your portal has somewhere to go:
The expanded Telport script which can go between levels or reload the same level:
- pragma strict
var levelToLoad : String = "";
var reload : boolean = false;
function OnTriggerEnter ( other : Collider ) {
if ( reload )
{
Application.LoadLevel(Application.loadedLevel);
}
if ( levelToLoad != "" )
{
Application.LoadLevel(levelToLoad);
}
}
Once you expand your level with more cubes for walls and jumping plus a portal to the next level, it might look like something like this: