gamecamp! and game dev @ davis introduction to scripting in unity®

30
GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Upload: jasmin-chambers

Post on 19-Dec-2015

231 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

GameCamp! andGame Dev @ DavisIntroduction to Scripting in Unity®

Page 2: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Create Standard 3D Project

Page 3: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Unity® Interface

Page 4: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Create Cube▼Create

►3D Object ►Cube

Page 5: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Add a Script ComponentName: ColorChanger

public Color color;

Void Start{GetComponent<MeshRenderer>().material.color = color;}

Page 6: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestSee that while the cube is white in the scene view, when the game runs, the cube changes to the assigned color

Page 7: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Add Another Script ComponentName: Mover

public Vector3 movementVector = Vector3.right;

Void Update{transform.position += movementVector * Time.deltaTime;}

Page 8: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestNotice that the cube only moves to the right indefinitely

Page 9: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Add More Code to Moverprivate Vector3 startPosition;

Void Start{startPosition = transform.position;}

Page 10: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Add More Code to MoverAfter transform.position in update

if (transform.position.x > startPosition.x + 1)movementVector = Vector3.left;else if (transform.position.x < startPosition.x – 1)movementVector = Vector3.right;

Page 11: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestNow the cube should move back and forth when the game is run

Page 12: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Make the Scripts InteractMake changes to ColorChanger

public Color color1;public Color color2;

Page 13: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Make the Scripts InteractRevise the Start code:

Delete: GetComponent<MeshRenderer>().material.color = color;

Page 14: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Make the Scripts Interactvoid Update () {

if (GetComponent<Mover>().movementVector == Vector3.left)GetComponent<MeshRenderer>().material.color = color1;

elseGetcomponent<MeshRenderer>().material.color = color2;

}

Page 15: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestThe cube should move back and forth and also change colors when changing directions

Page 16: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Create PrefabRename Cube in Hierarchy to SpecialCube

Drag SpecialCube into Assets

Create 2 more of these SpecialCubes from the prefab in Assets – drag from Assets into Hierarchy or Scene

Page 17: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestAll the cubes are identical in behavior

Page 18: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Individualize CubesChange colors and movement vectors (X only) in the Inspector

Bold means values are set to different parameters than the initial prefab

Default values set when making the prefab

Page 19: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestThe cubes should behave differently

Page 20: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Ball Rolling Tower Destroyer

Create a New 3D Project

Create:Terrain

Cube, add RigidBody componentSphere, add RigidBody component

Page 21: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Ball Rolling Tower Destroyer

Make sure cube and sphere are above terrain

Cube and sphere Ys should be set to .5 or higher

Use ‘W’ to show translation arrows

Page 22: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Ball Rolling Tower Destroyer

Add new script BallMover to sphere

http://pastebin.com/YpnvHxS2

Page 23: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Ball Rolling Tower Destroyer

Add SmoothFollow script from Imported Characters Assets – search in “Add Component”

Add as component to Camera, then assign follow target as sphere

Page 24: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestRoll ball around and knock the cube

Page 25: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Duplicate cubeMake 5 levels of 3x3 using CTRL+D (duplicate) and holding CTRL when repositioning (snap 1 unit of distance)

Page 26: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestTry destroying the tower… probably much harder to do

Page 27: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Adjust Mass of SphereIn RigidBody component to 10

Page 28: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestSphere moves slower, since it is heavier, but is more effective

Page 29: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

Adjust Mass of CubesGroup select all cubes (using CTRL or CMD)

Change mass to .15

Page 30: GameCamp! and Game Dev @ Davis Introduction to Scripting in Unity®

TestKnock over everything!