Lab 2 - Roll A Ball: Camera and Play Area
This assignment is due on Wednesday 4/11.
Continue off of Lab 1 for this assignment.
We will be following these video tutorials:
Part 1: Moving the Camera
First, we need to get the camera to follow the player.
- Select the camera, move it up 10 units and tilt it down 45 degrees.
- Change the Y Position and the X Rotation.
- Try making the Player the “parent” of the Main Camera.
- This is done by dragging the camera on top of the player in the Hierarchy View.
- Playtest the game. What happens?
- We need a way to have the camera follow the player without also rotating.
- This can’t be done with a simple “parent” operation, so let’s undo that.
-
Create a new script component on the Main Camera and call it
CameraController
- Add a public field
player
of typeGameObject
. -
Add a private field
offset
of typeVector3
. - In the
Start()
method, initialize offset to be the difference between the camera position and the player position.- Our controller has a field
transform
that has a fieldposition
so we can get the camera position usingtransform.position
. - The
player
GameObject also has thistransform
field. - What’s the difference between two vectors? Well, think about what we normally mean (mathematically) when we say “difference”
- Our controller has a field
-
In
Update()
, set the position of the camera to be the player position plus the offset we calculated inStart()
- Because this update depends on positions thats might be affected by physics update, we should use
LateUpdate()
instead - change the name of the method.
Part 2: Setting up the Play Area
Let’s add some walls so that the player can’t fall off the edge.
- Create a cube and set the scale to x =
0.5
, y =2
, z =20.5
. - Name the cube
WestWall
-
Slide the cube to the edge of the floor.
- Create three additional cubes to serve as the four walls of our play area.
- Use the Edit → Duplicate function to duplicate one of your walls - this will make the creation process faster.
- The east and west walls face the same direction, but north and south are different.
- Either rotate, or swap the scale parameters, to create the north/south walls.
- Playtest to confirm the player cannot fall off the map!