gamecamp! and game dev @ davis introduction to scripting in unity®
TRANSCRIPT
GameCamp! andGame Dev @ DavisIntroduction to Scripting in Unity®
Create Standard 3D Project
Unity® Interface
Create Cube▼Create
►3D Object ►Cube
Add a Script ComponentName: ColorChanger
public Color color;
Void Start{GetComponent<MeshRenderer>().material.color = color;}
TestSee that while the cube is white in the scene view, when the game runs, the cube changes to the assigned color
Add Another Script ComponentName: Mover
public Vector3 movementVector = Vector3.right;
Void Update{transform.position += movementVector * Time.deltaTime;}
TestNotice that the cube only moves to the right indefinitely
Add More Code to Moverprivate Vector3 startPosition;
Void Start{startPosition = transform.position;}
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;
TestNow the cube should move back and forth when the game is run
Make the Scripts InteractMake changes to ColorChanger
public Color color1;public Color color2;
Make the Scripts InteractRevise the Start code:
Delete: GetComponent<MeshRenderer>().material.color = color;
Make the Scripts Interactvoid Update () {
if (GetComponent<Mover>().movementVector == Vector3.left)GetComponent<MeshRenderer>().material.color = color1;
elseGetcomponent<MeshRenderer>().material.color = color2;
}
TestThe cube should move back and forth and also change colors when changing directions
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
TestAll the cubes are identical in behavior
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
TestThe cubes should behave differently
Ball Rolling Tower Destroyer
Create a New 3D Project
Create:Terrain
Cube, add RigidBody componentSphere, add RigidBody component
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
Ball Rolling Tower Destroyer
Add new script BallMover to sphere
http://pastebin.com/YpnvHxS2
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
TestRoll ball around and knock the cube
Duplicate cubeMake 5 levels of 3x3 using CTRL+D (duplicate) and holding CTRL when repositioning (snap 1 unit of distance)
TestTry destroying the tower… probably much harder to do
Adjust Mass of SphereIn RigidBody component to 10
TestSphere moves slower, since it is heavier, but is more effective
Adjust Mass of CubesGroup select all cubes (using CTRL or CMD)
Change mass to .15
TestKnock over everything!