Assignment 2: Build the SABER

In this assignment, you will build the lightsaber and its basic SUPER DESTRUCTIVE capabilities. Before starting this one, you should have completed the assignment from Berkeley.

Create the Saber

Click "Create" in the Hierarchy and select "Create Empty" to make a parent transform to hold our lightsaber.

Name it "Lightsaber" and set the position to 0,0,0.

Click "Create" and add a Cylinder to the scene for the handle of the lightsaber.

Name the cylinder "Handle" and set the position to 0,0,0.

In the Hierarchy, drag the Handle ONTO the lightsaber to make the Handle a child of the Lightsaber transform. Double check that the transform position for both Lightsaber and Handle are both at 0,0,0.

Now, take creative license and make your lightsaber unique. Maybe give it a fancy handle with jewels, or maybe turn it into an axe.

Reminder! To add colors, go into your project view. Inside "Assets," create a folder called "xMaterials" and go into that folder. Right click or click "Create" in the project view, and then click "Material." Change the little colored square labeled "Albedo."

REMEMBER TO SAVE YOUR SCENE BY HITTING CTRL or CMD + S.


Create the Saber Script

In your projects view, click "Create" or right-click and create a "Folder."

In your projects view, name that new folder "xScripts" and go into it

In the xScripts folder, click "Create" or right-click and create a "C# Script." Name it "SaberControl." Double click it to open it.

It will look something like this.

Delete the Start() and Update() functions and replace it with the following code. OnTriggerEnter is a "Trigger Event." When our script is attached to an object, the function OnTriggerEnter will be called whenever an object with a collider hits our object, given that we have the following components on our object: A collider that is a TRIGGER, and a rigidbody. Hit Ctrl + S (Windows) or CMD + S (Mac) to save the script.

Back in Unity, select your Lightsaber and click "Add Component". Search for "Capsule Collider" and select it. MAKE SURE IT IS NOT the 2D one.

You can now see the capsule collider looks like this little green sphere. We want to shape it to cover the shaft of the saber (or whatever sword/axe/bamboo stick you made).

Over in the inspector under the capsule collider, you can adjust these values so that the capsule collider changes shape and position. Adjust it until the collider looks like it pretty much matches the shape of your saber.

For Trigger Events to be captured, we also need a rigidbody. Rigidbodies let an element implement physics on other objects.

However, we don't want our lightsaber falling out of our hands, so let's turn gravity off. We also want to the lightsaber to move according to OUR code, not the physics engine's, so we'll turn it to kinematic (no physics acts on the motion of this object).

Last thing we need for our OnTrigerEnter to fire is an actual trigger. So let's set our lightsaber's capsule collider to a TRIGGER.

Now that we have a trigger collider and a rigidbody on our object, let's add our SaberControl script. On the Lightsaber GameObject, click "Add Component" and look up "SaberControl." Now add it to the object.

Now that we have a trigger collider and a rigidbody on our object, let's add our SaberControl script. On the Lightsaber GameObject, click "Add Component" and look up "SaberControl." Now add it to the object.

Your screen should look similar to this now.

You can adjust the layout so that the Game view is side by side with the Scene view.

REMEMBER TO SAVE YOUR SCENE BY HITTING CTRL or CMD + S.


Test the Saber

Create a cube to put in the scene. Position it at 0, 1, 3.

Now, hit play and move the lightsaber into the cube.

REMEMBER TO SAVE YOUR SCENE BY HITTING CTRL or CMD + S.

Congrats! You've finished homework 1. Please take a screenshot of your creative lightsaber and upload it to the Google Drive folder called "HW1" as LASTNAME_FIRSTNAME_SABER.[jpg/png] (e.g LE_KHOI_SABER.png). Make sure to write a reflection on Canvas as well!

Assignment written by Miriam Haart, Kevin Chang, Khoi Le, Ethan Aldrich and Connor Settle, January 2019.