Saturday, May 7, 2016

Unity Tutorials: Roll-a-ball

Unity Roll-a-ball Tutorial

This is a very basic Unity tutorial aimed at complete beginners. (Me since I haven't touched Unity in four years ...)

The condensed steps:

1. Create a new project. Call it "Roll-A-Ball". (Make sure it is a 3D project.)

Goal: Create a "Roll-A-Ball" game where we can roll a ball around and "collect" objects.
We will move the ball using physics based on user input. We will see how to detect contact between objects and the player, and how to display the count of picked up objects.

No models, textures, sounds or animations.

2. Create a plane. Make sure the transform is at (0, 0, 0).

3. Create a sphere to be the player. Rename it player. Make sure the transform is at (0, 0.5, 0).

4. Create a material that is colored. Put it in an "Materials" folder. Name the material "background".
Drag the material onto the plane in the scene viewer. This adds the material as an component to the Plane.

5. If necessary, rotate the Directional Light created by default so that the player has better lighting.

Note: Use the "Gizmos" dropdown in the scene viewer to turn on and off the scene grid.
Note: Use "F" to focus on the currently selected object in the scene
Note: There are three ways to adjust transforms. You can use the respective *tools* (toolbar in the upper-left corner of the screen - can switch between hand-scroll, move, rotate, scale, and deform).
You can hover over the label in inspector and drag the mouse left or right. Finally, you can type the number directly into the inspector.

Note: A plane has no volume. The y-axis only determines which way the plane faces.

6. Setup the camera. Tie the camera to the player object. (Since the ball is rotating we can't do this.)
Lift it up and tilt it down.

Note: Children of game objects move and rotate with their parent game objects

7. Use a script to associate the camera's position with the player. On every LateUpdate (), move the camera to a position that is a fixed offset from the Player. Determine what this fixed offset is in Start().

Note: Get your transform using "transform" (i.e. this.transform) and the transform's of other objects
using <otherobject>.transform.

8. Add walls to the scene. Create a Cube and scale it appropriately so that you have four walls.

9. Create collectable pickup objects.

9a. Create a Cube and design it the way you like.
9b. Create a Rotater script which rotates the transform every Update. Attach this script to the cube.
9c. Rename this cube and make it into a Prefab. Copy it around the scene!
9d. Create an empty GameObject to serve as the "folder" for these pickups.
 
Note: Tired of looking at a certain GameObject? Want to temporarily make it go away? Deactivate it in the Inspector by deselecting the checkbox next to it's name.
Note:
The easiest way to focus on an object is to double-click on it in the scene hierarchy. Screw using the F button!

10. Detect collisions.
Check the "Is Trigger" on the box collider of our pickup prefab.
Declare a tag and write pickup code.

Note: To add a tag look at the Object Inspect at the drop-down under the name of the object.

11. Setup a simple GUI.
Create -> UI -> Text

Note: UI elements must be the child of a Canvas element.
Note: The transform of UI elements is a special "Rect Transform"

Wire up the UI element. Add using UnityEngine.UI Create a public winText.
Set it to the empty string in Start(). In the game editor associate the GameObject winText with the player script.
Make it so that when count goes above the target number winText is set to a win message.

12*. Build the game.
Go to build settings.
Add desired scenes to "Scenes to Build"

* My Linux stand-alone build isnt' working for me on Ubuntu 14.04.

No comments :

Post a Comment