Lab 1 - Roll A Ball: Environment and Player
This assignment is due on Friday 4/6.
For the first few lab assignments, we will be building the “Roll a Ball” game following the official Unity tutorials. These are video tutorials, so I will include textual and simplified instructions but you can also watch the video if anything is unclear or if you would like further explanation.
Before you start, you should familiarize yourself with the Unity Editor Layout. Not included in that article is the group of standard drop-down menus that are above the editor (File, Edit, etc.) which is called the Menu Bar.
Part 1: Scene Setup
- Create a new project (3D).
-
Create a new scene and save it (recommended: in a folder called
_Scenes
). - Create a 3D Plane game object and name it
Ground
.- There are a number of ways to create a game object.
- You can right click on the Scene or anywhere in the Hierarchy Window.
- You can click on “Game Object” in the Menu Bar.
- To rename the game object, click twice (slowly) on it in the Hierarchy Window or right click → Rename.
- Resize the plane to be twice as wide in the X and Z directions.
- Select the Ground in the Hierarchy Window, or just click on it in the Scene View.
- Now you can change its details in the Inspector Window.
- Find “Scale” under the “Transform” component and change the X and Z values to
2
.
- Create a new material with a dark color.
- Make a folder called
Materials
in the Project Window.- Right click on “Assets” → Create → Folder
- In that folder, create a Material and name it
Background
- With the Material selected, find “Albedo” in the Inspector Window and change the color to something dark but not totally black.
- Make a folder called
-
Attach the new material to the ground object. You can drag the material from the Project Window to the Scene View.
-
Change the directional light Y rotation from
-30
to60
-
Create a 3D Sphere game object and name it
Player
. With this object selected, you can press F in the Scene View to focus on this object.
Part 2: Moving the Player
- Create a Rigidbody component and attach it to the Player object.
- You can use the “Component” menu in the Menu Bar, or select “Add Component” in the Inspector Window.
- Either way, the Player game object needs to be selected in the Hierarchy Window.
- Component → Physics → Rigidbody
- Create a script component on our Player object.
- Component → New Script
- Set the name to
PlayerController
and the language toC sharp
-
Move it to a folder called
Scripts
in the Project Window. -
Open the script from th Project Window - it should open in either Visual Studio (Windows) or MonoDevelop (OS X, Linux).
- We need access to the
Rigidbody
component of this object, so let’s make a variable to hold it:
private Rigidbody rb;
- Initialize this variable in the
Start()
method using theGetComponent<>
method:
rb = GetComponent<Rigidbody>();
- We are going to add some Physics code, so we’re going to add a
void FixedUpdate()
method to this class. - In the
FixedUpdate()
function, get some input from the player using theInput
class and save it in some local variables:
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
- We want to move the player using these movement inputs. We can use the
Rigidbody.AddForce
function, but it requires aVector3
:
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement);
- Compile, then go back to the Unity editor and press the button. You can use the WASD buttons to move the ball, or an attached gamepad controller. If we try this out, we’ll see that the ball moves pretty slowly and is not easy to control. Let’s go back to the script and add a way to change the speed of the ball.
- First, add a
public float speed;
to the class. - Now, use this variable to control the force added to the Rigidbody:
rb.AddForce(movement * speed);
- If we compile and head back to the editor, we’ll see that with the Player object selected in the Hierarchy Window, there is a Speed field under the Player Controller (Script) that we can change. Try some values and pick something that feels right!