Gamebridge/Coin Tutorial: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
Line 83: Line 83:


There are a ton of ways to improve this basic game. You could trigger a noise when the Player touches the Coin. You could make a 3D model for the Player, and move the camera back to make the game in 3rd person. And you could make a counter that increases by 1 every time the player collects a coin.
There are a ton of ways to improve this basic game. You could trigger a noise when the Player touches the Coin. You could make a 3D model for the Player, and move the camera back to make the game in 3rd person. And you could make a counter that increases by 1 every time the player collects a coin.
= The following can be deleted. =
== 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.cs script
* Create a World.cs script
* Make Coin.cs a trigger that sends an AddCoin message to World.cs
* Make World.cs increment a coin integer variable and display it OnGUI.
== Make a coin object ==
You need an actual 3D game object to represent coins in the game.
# In the Hierarchy panel, click Create.
# Click 3D Object.
# Click Cylinder.
=== Size the cylinder to look like a coin ===
# Click the Scale tool or press the R key.
# Double click on the cylinder's name in the Hierarchy or type F key to focus on the cylinder, which zooms into it
# In the inspector panel, the scale's Y value defaults to 1. Set it to be 0.1.
# Now it looks like a flat, circular coin, a doubloon!
=== Color the coin to be golden ===
* Attach the Coin.cs script to

Revision as of 08:46, 14 November 2015

Coin Collecting Tutorial

This tutorial will teach you how to make a simple game with a coin that will disappear when your player touches it, just like in Super Mario Bros.

First you need to set up your project in Unity:

  1. Open Unity on your computer.
  2. After the Project window pops up, click the New button to create a new project.
  3. In the Project Name field, replace "New Unity Project" with "Coin Collector".
  4. Click the Asset Package button.
  5. Another window will pop up. Click the Characters asset package, then click the Done button.
  6. Click the Create Project button.
  7. Wait for Unity to load your project.

Setting up 3D Models

Now that your project is set up, you need to create the 3D objects that will make up your game. You will make a ground, a coin, and set up your player.

Ground:

  1. In the Hierarchy window, click the Create button. Hover your mouse over 3D Objects, then click Plane.
  2. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset. It's good practice the reset a game object after creating it.
  3. In the Hierarchy window, right-click Plane, then click Rename. Rename the Plane to Ground.

Coin

  1. In the Hierarchy window, click the Create button. Hover your mouse over 3D Objects, then click Cylinder.
  2. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset.
  3. In the Hierarchy window, right-click Cylinder, then click Rename. Rename the Cylinder to Coin.
  4. In the Inspector window, set the Y and Z Position to 4 in the Transform component.
  5. Also in the Transform component, set the Y scale to 0.1.
  6. Then set the X Rotation to 90 and the Y Rotation to 45

Player

Your game will have a first person view, so you don't have to make a 3D Object for the player. Instead you will give the Main Camera a Character Controller and a Collider.

  1. In the Hierarchy window, right-click Main Camera, then click Rename. Rename the Main Camera to Player.
  2. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset.
  3. In the Inspector windows, set the Y Position to 1 in the Transform component.

Setting up Player Input and Collision

Now you need to give your Player the ability to move around in your game. You will also give your Player a Collider, which will allow your player to interact with other 3D Objects.

  1. In the Project window, click the search bar, then type FPSController
  2. Drag the FPSController prefab to Player in the Hierarchy window. FPSController is a collection of scripts that comes with Unity.
  3. The FPSController prefab should now be a Sub-Object under Player in the Hierarchy window.
  4. In the Hierarchy window, Click the FPSController prefab under Player.
  5. In the Inspector window, click the little gear in the top right corner on the Transform component, then click Reset.
  6. In the Hierarchy window, click the Player object.
  7. In the Inspector window, click the Add Component button.
  8. A new window will pop up. Click Physics, then click Sphere Collider.
  9. Click the Play button above the Scene view. At this point, your game should be playable. Use the mouse to rotate your Player's view. Press the W, A, S, D keys to move your Player. And press the spacebar to jump.
  10. When you are done making sure it works, click the Play button to stop the game.

Adding Color to the 3D Objects

  1. In the Project window, click Create, then click Material.
  2. Go back to the Project window and create another Material.
  3. Click New Material, then click the small white box in the Inspector window. It's under Main Maps, to the right of the word "Albedo".
  4. A new window will pop up. Mess around until you see the color green.
  5. Exit out of the window.
  6. In the Project window, drag New Material to Ground in the Hierarchy window.
  7. In the Project window, click New Material 1, then click the small white box in the Inspector window.
  8. Make a yellow color, then click exit out of the color picker.
  9. In the Project window, drag New Material 1 to Coin in the Hierarchy window.
  10. At this point, the Coin should be yellow and the Ground should be green.

Writing a Coin Collector script

Now you need to write a script that makes the coin disappear when the player touches it.

  1. In the Project window, click the Create button, then click C# Script.
  2. In the Project window, double-click NewBehaviourScript
  3. MonoDevelop is a separate program that you will use to write your script. Wait for it to open.
  4. In your script you will use a piece of code that will make the Coin disappear when the Player Collider touches the Coin Collider.
  5. Copy the code below and paste it between the two curly braces at the end of the script in MonoDevelop.
   void OnTriggerEnter(Collider other) {
       Destroy(gameObject);
   }
  1. This code uses a method called OnTriggerEnter to destroy the Coin when the Coin's collider touches the collider of another object.
  2. Press Ctrl and S at the same time to save the script.
  3. Go back to Unity.
  4. In the Project window, drag NewBehaviorScript to Coin in the Hierarchy window.
  5. In the Hierarchy window, click Coin.
  6. In the Inspector window, find the Capsule Collider component, then click the box next to Is Trigger. You have to do this to make the coin an object that can trigger an event.

Play!!!

  1. Click the Play button on top.
  2. Collect the Coin by jumping and touching it.
  3. After the Player touches the Coin, the Coin will disappear in both the Game window and the Hierarchy window.

There are a ton of ways to improve this basic game. You could trigger a noise when the Player touches the Coin. You could make a 3D model for the Player, and move the camera back to make the game in 3rd person. And you could make a counter that increases by 1 every time the player collects a coin.