62
By Shahed Chowdhuri Senior Technical Evangelist Unity 5: Rollerball (Step by Step) Based on Roll-a-ball video tutorial from Unity Technologies Part 1 @shahedC WakeUpAndCode.com

Rollerball: 1 of 2

Embed Size (px)

Citation preview

Page 1: Rollerball: 1 of 2

By Shahed ChowdhuriSenior Technical Evangelist

Unity 5: Rollerball (Step by Step)Based on Roll-a-ball video tutorial from Unity Technologies Part 1

@shahedC

WakeUpAndCode.com

Page 2: Rollerball: 1 of 2

Agenda

Getting Started

> Creating the Ground> Creating the Player> Materials, Colors & Lighting> 3D Physics & Movement

Controlling the Camera

Page 3: Rollerball: 1 of 2

Getting Started

Page 4: Rollerball: 1 of 2

Create New Project

… you should see a New Project window

When you launch Unity 5…

Page 5: Rollerball: 1 of 2

File New Project…

Click File New Project

If you don’t see a New Project window…

Page 6: Rollerball: 1 of 2

Create Project

Select 3D (not 2D)

Enter the following:• Project name: Rollerball• Location: (any new/empty folder)

Click [Create project]

Page 7: Rollerball: 1 of 2

Choose a Layout

Hint: You can drag the panels (Scene, Game, etc) to different locations.

Click Layout dropdown to selector save

Page 8: Rollerball: 1 of 2

Save Your Scene

Hint: You can also click Ctrl-S on your keyboard.

ClickFile Save Scene

Page 9: Rollerball: 1 of 2

Create “Scenes” SubfolderCreateNew folderbelowAssets folder

Name it“Scenes”

Page 10: Rollerball: 1 of 2

Name Your Scene

, then click [Save]… Name your scene “PlayingField.unity”

Inside“Scenes”Subfolder…

Page 11: Rollerball: 1 of 2

Verify Saved Scene

Verify Saved Scene in Project panel

Verify Scene Name in Title Bar

Page 12: Rollerball: 1 of 2

Creating the Ground

Page 13: Rollerball: 1 of 2

Create a Plane in 3D

Click GameObject 3D Object Plane Click Create 3D Object Plane

In the top menu… In the Hierarchy panel…- OR -

Page 14: Rollerball: 1 of 2

Verify Plane in Scene & Hierarchy

Page 15: Rollerball: 1 of 2

Rename Plane to “Ground”

Right-click to rename

Enter “Ground”

Page 16: Rollerball: 1 of 2

Rename Plane to “Ground”

Select “Ground”in Hierarchy

Click gear iconnext to Transformthenclick “Reset”

Page 17: Rollerball: 1 of 2

Verify Ground Position at Origin (0,0,0)

Page 18: Rollerball: 1 of 2

Focus on Ground in SceneWith “Ground” selected in hierarchy…

… press ‘F’ on your keyboard to focus on it

… move your cursor to the Scene

Page 19: Rollerball: 1 of 2

Zoom In/Out Within Your Scene

Scroll the mouse wheel over your scene to zoom in/out

Page 20: Rollerball: 1 of 2

Show/Hide Grid in Scene ViewIn the Scene panel, click the Gizmos dropdown to toggle “Show Grid”

Page 21: Rollerball: 1 of 2

Resize the GroundClick on the Scale tool, while the Ground is selected…

… then, drag the handles to resize the ground

Page 22: Rollerball: 1 of 2

Enter Scale ValuesManually enter Scale values in the Ground’s Transform component

Scale: X = 2, Y = 1, Z = 2

Page 23: Rollerball: 1 of 2

Creating the Player

Page 24: Rollerball: 1 of 2

Create a Sphere in 3D

Click GameObject 3D Object Sphere Click Create 3D Object Sphere

In the top menu… In the Hierarchy panel…- OR -

Page 25: Rollerball: 1 of 2

Position Sphere on Ground

Position Sphere at X = 0, Y = 0.5, Z = 0

Page 26: Rollerball: 1 of 2

Rename Sphere to “Ball”

Page 27: Rollerball: 1 of 2

Materials, Colors & Lighting

Page 28: Rollerball: 1 of 2

Create New Folder for “Materials”

In the Project panel,click Create dropdown

then, select “Folder”

Page 29: Rollerball: 1 of 2

Create New Folder for “Materials”In Project panel,click Create

then, select “Folder”

Rename it “Materials”

Page 30: Rollerball: 1 of 2

Create New Materials for Ground & BallIn Project panel,

click Create

then, select “Material”

Rename it “GroundMaterial”Repeat for “BallMaterial”

Page 31: Rollerball: 1 of 2

Update Material Colors

For each material...

Update colorin Inspectorwindow

(click white square)

Page 32: Rollerball: 1 of 2

Choose Colors in the Popup

• Red• Green• Blue• Alpha (Transparency)

Page 33: Rollerball: 1 of 2

Verify Colors

Verify that GroundMaterial and BallMaterial have different colors.

Page 34: Rollerball: 1 of 2

Assign Colors

Drag each material from Project panel to the Scene panel

Page 35: Rollerball: 1 of 2

Update Lighting Rotation

Update Direction Light in InspectorRotation: X = 50, Y = 60, Z = 0

Page 36: Rollerball: 1 of 2

3D Physics & Movement

Page 37: Rollerball: 1 of 2

Add RigidBodyWith “Ball” selected in the Hierarchy panel…

Click Component Physics RigidBody

Page 38: Rollerball: 1 of 2

(Alternate Method) Add RigidBody

With “Ball” selected in the Hierarchy panel, click Add Component Physics Rigidbody

Page 39: Rollerball: 1 of 2

Verify RigidBody Component for Ball

Verify RigidBody Component

Page 40: Rollerball: 1 of 2

Create New Folder for “Scripts”In the Project panel,click Create dropdown

Then select “Folder”, name it “Scripts”

Page 41: Rollerball: 1 of 2

Create “PlayerController” ScriptIn the Project panel,click Create dropdown Then select “C# Script”, name it

PlayerController

Page 42: Rollerball: 1 of 2

Assign “PlayerController” to Ball

With “Ball”selected...

… Dragscript intoBall

Verify Script Component!

Page 43: Rollerball: 1 of 2

Launch “PlayerController” Script

Page 44: Rollerball: 1 of 2

Get User Input

Add FixedUpdate() method

… just before last curly brace

Page 45: Rollerball: 1 of 2

Calculate 3D Movement

Inside FixedUpdate() method…

Calculate 3D movement

Page 46: Rollerball: 1 of 2

Add RigidBody Force

Define rbjust before Start()

Initialize inside Start() method

Add Force insideFixedUpdate()

Page 47: Rollerball: 1 of 2

Run the Game… too slow?Click Play button, move around with arrow keys!

Page 48: Rollerball: 1 of 2

Add Variable for Speed

Define publicspeed variable

Multiply speedwith movement

Page 49: Rollerball: 1 of 2

Assign Speed in Unity

With “Ball”selected...

Set Speed = 10

What happens if

you set it to 100?

Page 50: Rollerball: 1 of 2

Run the Game… better now?Click Play button, move around with arrow keys!

Page 51: Rollerball: 1 of 2

Controlling the Camera

Page 52: Rollerball: 1 of 2

Set Camera Position & Rotation

Set Camera Position and Rotation

• Position: X = 0, Y = 10, Z = -10• Rotation: X = 45, Y = 0, Z = 0

Page 53: Rollerball: 1 of 2

Verify Camera View in Game

Camera should appear higherwith a better view

Page 54: Rollerball: 1 of 2

Create “CameraController” ScriptIn the Project panel,click Create dropdown Then select “C# Script”, name it

CameraController

Page 55: Rollerball: 1 of 2

Launch Script for “CameraController”

Page 56: Rollerball: 1 of 2

Define Instance Variables

Define instancevariablesjust before Start()

Page 57: Rollerball: 1 of 2

Initialize Camera-Player Offset

Initialize offsetin Start() method

Page 58: Rollerball: 1 of 2

Set Camera Position in LateUpdate()

Add LateUpdate()method

Then, setcameraposition

Page 59: Rollerball: 1 of 2

Assign “CameraController” to Camera

With “Main Camera”selected...

… Dragscript intocamera

Verify Script Component!

Page 60: Rollerball: 1 of 2

Assign “CameraController” to CameraWith “Main Camera”selected...

… Drag Ball into Camera’s Player object

Page 61: Rollerball: 1 of 2

Run the Game! Click Play, move around!

Page 62: Rollerball: 1 of 2

End of Part 1