Editing
Gamebridge/Trigger Tutorial
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== 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 ) {<br> <br> } # # '''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:<br> <br> function OnTriggerEnter( other : Collider ) {<br> loadlevel<br> }<br> ## 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 "}".<br> Your code should look like this:<br> <br> function OnTriggerEnter( other : Collider ) {<br> Application.LoadLevel("Highscore"); <br> }<br> ## Change Application.LoadLevel("Highscore"); to "Application.LoadLevel("level1");"<br> Your code should look like this:<br> <br> function OnTriggerEnter( other : Collider ) {<br> Application.LoadLevel("level1"); <br> }<br> ## 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! #
Summary:
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Noisebridge:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Request account
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Dig in!
Noisebridge
Status: MOVED
Donate
About
Accessibility
Vision
Blog
MANUAL
Visitors
Participation
Community Standards
Channels
Operations
EVENTS
GUILDS
Meta
Electronics
Fabrication
Games
Music
Library
Neuro
Philosophy
Funding
Art
Crypto
DOCUMENTATION
Recent Changes
Random Page
Help
Categories
Edit
Tools
What links here
Related changes
Special pages
Page information