Unity3D Tutorial #4.1

Throw A Ball

Open up Unity and click "New" to create a new Unity Project. Make sure the 3D option is selected, and Create Project. Lets give it a name like, "Throw A Ball".


This should bring us to the project editor.

Hierarchy

On the left is the Hierarchy menu. Here is where all objects in the scene go.

Click on anything in the Hierarchy menu, hover your mouse over the scene view, and type "f" to focus on the object.

The project editor's default view should look a little like this (If you aren't in default view, you can change the view by going to (Window>Layouts). I'll go over all the views we will be using and what they each do. It's okay if you don't understand it now, I'll go into more depth as we use each menu.

Project Window

On the bottom is the project window. This is where you organize all your files and folders as you work.

Inspector

To the right is the inspector menu.


When an object is selected, you can use this to change attributes specific to that object.

Let's do some organizing before we start. Save the scene (File>Save Scene), or (Ctrl+S) and give it a meaningful name (I named it Main). The default save folder should be the Assets folder. The saved scene should look like the Unity logo.

If we save everything in the Asset's folder, our projects will get pretty messy, so let's organize our files into folders. Navigate to the Assets folder, then right click the project view (shown below), and add a new folder. Let's call it "Scenes". Now drag the scene that we just created and drop it into the Scenes folder.

Before we add any virtual reality to our project, lets set up the scene. Go to the Hierarchy menu, right click, and in the popup menu do (3D Object>Plane). This will give us something to stand on when we start the project. Calling it plane sounds a little too...plain, right? To rename something in Unity, click it twice, but space out the clicks. Change the name to "Floor"

Make sure that the floor's transform properties in the Inspector are all zero'ed and the scale is all set to 1. In addition, make sure the floor is not underneath anything in the hierarchy menu (If so, drag it out from under the parent object).

This will place the plane flat in the center. If not, to easily reset the properties, click the gear in the top right corner of the Transform box, and click "Reset"

Let's now add some things to interact with! Right click the Hierarchy menu and navigate 3D Object>Sphere.

Let's rename the Sphere to "Ball". Soon, we'll be able to throw this ball! Be sure that the ball's position is zero'ed out.

Scaling the ball at 1x1x1 makes it about the size of a beach ball. Let's shrink it down a bit. If we change the scale of x, y, and z to 0.25, that should make the ball about the size of a softball. Let's also raise it off the ground a bit. Type in (x:0, y:1.125, z:1) into position. These numbers are a little specific, but they'll come in handy in the future.

Now let's add a little bit of physics to our project. One of the cool things about Unity is the fact that adding physics to a project is almost a click of a button away.

Find the "Add Component" Button at the bottom of the inspector menu. There, a search menu should pop up. Type in "Rigidbody" (note: not Rigidbody 2D) and hit Enter. Make sure Use Gravity is checked, and Is Kinematic is unchecked. Now if you click the "Play" button at the top of the scene menu, you won't have to wait until January to see the ball drop.

In the same way we created the Floor and Ball, add a Cube, and name it "Table", and adjust its transform to the properties below

The ball should now be resting comfortably on the table.

Now a little bit on the reasoning for the numbers: When we add in the virtual reality cameras, the player will start out facing the z-axis. By putting the table and ball 1 unit out, when the application starts, the table and ball will be right in front of the player. Since the "table" is 1 unit high, when its position is set to 0,0,0, you might have noticed that half of it was above, and half of it below (similar to the ball). Therefore, if we raise an object by half its height, we will put the object flat on the floor, along the x,z-plane.

The other adjustments to scale are just to make it more table-y in the opinion of the esteemed author for this tutorial.

Let's make something to throw our ball at. Add a cylinder to the hierarchy menu, and name it "Target".

The cylinder looks pretty big! Let's shrink it down a little and put it at the edge of the floor.


Now you must be thinking, "my scene looks whiter than a flock of doves eating rice-covered marshmallows in the middle of a blizzard," and you are absolutely right! Let's change that, shall we?

Create a new folder in "Assets" and name it "Materials" (refer to the "Save Scene" section for a refresher). Navigate to that folder and create a new Material (Create>Material). Let's name it "Ball," since we will use this to make our ball look extra sleek and schwifty. In the material's inspector, we can change all sorts of attributes relating to how something looks. There should be a dropper in the upper area of the inspector. Clicking that will allow us to pick a color. I picked red, but I encourage you to go as wild as you can when it comes to picking ball colors. In the inspector, there are also sliders that can make make the material look more shiny/metallic, or look smoother.

Once you are satisfied with how the material looks, drag the material from the Materials folder to the the desired object in the hierarchy menu (in this case, it would be ball). The object should now have the texture and color in the scene view.

Let's make some materials for the rest of the objects in the scene to give the project some extra personality!

Now for the moment you've all been waiting for... it's time to add some VR!!!

A little warning though, if you don't have a headset system to test with, you won't be able to run the program until after class.

In the top toolbar, navigate to the Asset Store, which should be one of the tabs above the scene (or alternatively, Ctrl/Cmd + 9). In the search bar, look for "SteamVR plugin" (Should look like the picture on the top right.)

Clicking it should bring you to the SteamVR plugin import screen on the right below the first. Click import. There should be a popup asking if you want to "Import Complete Project." Click import again.

A popup should appear that says "Import Unity Package" on the top, and should have a bunch of checkboxes. This is where you can choose to selectively import parts of an asset package without importing the entire thing. Ignore the package and click import a third time.

After a minute or so, there should be a SteamVR folder in the Asset folder. Navigate into the SteamVR Prefabs folder (SteamVR>Prefabs). Now delete the "Main Camera" in the hierarchy (it will interfere with CameraRig), and drag "CameraRig" and "SteamVR" into the hierarchy.

CameraRig represents the player's point of view when the project is running, and controls the controllers and headset.

SteamVR helps smooth out the application at runtime.

There should be an arrow on the left that shows child elements of CameraRig. Click it, and it should reveal Controller (left), Controller (right), and Camera (head). Ctrl/Cmd + Click both controllers. This allows us to change both at the same time.

Since we want our hands to also be able to interact with physical objects, we also want our controllers to also have rigidbodies and box colliders.

Add a rigidbody and box collider component in the inspector menu. In the rigidbody component, uncheck Use Gravity, and check Is Kinematic. After all, we don't want our hands falling down, do we? By checking Is Kinematic, we are telling Unity to treat the rigidbody as a physical object, but not have it abide by the laws of physics.

Collisions in Unity are not the same as real life collisions. As long as two objects are touching or overlap, they count as "colliding". Check Is Trigger. Adding a box collider and making it a trigger, we are basically telling Unity to notice every time the collider touches something. The default box collider is pretty big though, and it would be kind of weird if we could pick up things without touching them. To get a realistic size that doesn't break immersion as we grab objects, we'll need to fine tune the box collider a bit.

Now if we are planning on working with Vive controllers, then we want the following specifications:

  • Center: x: 0, y: -0.04, z: 0.02

  • Size: x: 0.14, y: 0.07, z: 0.05

If we are planning on working with Oculus controllers, then opt for the following specifications instead:

  • Center: x: 0, y: -0.02, z: -0.06

  • Size: x: 0.05, y: 0.05, z: 0.05

Go to Window>SteamVR Input. You will most like get a dialog explaining that you're missing an actions json and asking if you'd like to use the default. Select "Yes."

Doing so will copy the default file, which will give us the standard set of actions associated with the controller inputs. What does that mean, you ask?

With the new input system, you are essentially assigning "actions" to buttons on the controller, almost like in video games where you can usually go to the settings to change the controls. In this way, you no longer have to think of "grabbing" in terms of pressing the trigger, but rather as just "grabbing."

After Unity finishes copying the file, the window on the right should pop up. As you can see, the "In" and "Out" Actions sections have already been populated. These are all the actions built into the default file, and the one we will be using in this tutorial is "GrabPinch." GrabPinch has already been associated with the trigger button on the Vive Controller, we won't be needing to change anything in this window.

Click "Save and generate." This will take a minute or so. After generating a new actions file, we can then use these actions in our projects!

As a side note, something I want to bring attention to is the Open binding UI button. Unfortunately, we can't do any binding without a vive headset attached, however, if you have the opportunity, I encourage you to play around with the binding UI on your own. The way it works, is you press the "+" to add a new action, which will then be available in the binding UI, where you select which button on the controller to associate with that action.

Now its time to code! Navigate to the Assets folder, and make a new folder called "Scripts". Now enter the folder and create a new C# Script (Create>C# Script), and name it "ControllerGrab". Double click the script, and it should open up an editor for C#.

The script default layout should look something like this:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class ControllerGrab : MonoBehaviour {


// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

}

}

The "using" keyword works similar to the way the "package" keyword works in Java. It gives a class file access to all classes under the System.Collections namespace, and the System.Collections.Generic namespace. These statements are auto-generated, and should not need to be changed.

Next is the line "public class ControllerGrab : MonoBehaviour {"

Unity uses the same keywords for levels of access as Java (Ex: public, private, protected, etc.). The colon represents inheritance, and can be used in place of both the "extends" and "implements" keywords. Multiple inheritance would looks something like this: "class ChildClass : ParentClass, Interface1, Interface2, Interface3". Lastly, MonoBehavior is a class that all Unity scripts must extend, we will go into a little more depth about the added functionality of this class in a bit.

We'll need to add the Valve.VR namespace in order to use much of the useful functionality of SteamVR.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using Valve.VR;

Let's start out by adding the variables that we will be using in our project. In the line after the class declaration line, type the following variables:

private GameObject collidingObject;

private GameObject held;

public SteamVR_Action_Boolean grabPinch;

public SteamVR_Input_Sources inputSource;

So many weird classes and objects right? Whats GameObject? What are the really long SteamVR class names? Let's break this down to what actually happens when the code runs.

  1. CollidingObject will be used to store any interactable object that the controller box collider is currently touching, or "colliding" with

  2. held will be used to store any object that is currently being held when we pick up an object with the controller.

  3. Since the only thing we need to know about the trigger button on the controllers is whether the button is pressed or not, we can represent the button state with a boolean, which is essentially what the SteamVR_Action_Boolean class does

  4. SteamVR_Input_Sources refers to which controller (left or right) the input is coming from.

On a separate note, before we continue, a little on functions such as Start(), Awake(), Update(), etc. MonoBehavior has a couple specific built in functions that already have assigned significance to Unity. Awake(), for example, executes only once as soon as the application starts up, before any other function is executed. After Awake methods are called, Start() is called. Any code written in the Update method body will be executed every frame of the program.

Delete the empty Start() function. We will not be needing it in ControllerGrab.

Now onto the next methods we will be needing- add these after the functions we wrote in the previous section.

private void SetCollidingObject(Collider col)

{

if (!held && col.GetComponent<Rigidbody>())

{

collidingObject = col.gameObject;

}

}


The SetCollidingObject method should be relatively straightforward. We basically for two things: are we already holding an object with the controller, and does the object we are colliding with have a RigidBody (Is it interactable)? If it is, then store the reference to the game object associated with the collider for the object.

Something interesting to note here is that instead of writing if (held != null), we can just say if (!held). In C#, the system can infer a null check, treating null as false and not null as true.


Next, we add 3 functions related to the box collider trigger. Just like Start() and Update(), the "OnTrigger" methods also have associated significance (as in, you have to spell them exactly as is, and they have to take in the right types of parameters). These 3 methods are called whenever our controller box colliders, which we checked the box "Is Trigger" for, touches or overlaps with something.

public void OnTriggerEnter(Collider other)

{

SetCollidingObject(other);

}


public void OnTriggerStay(Collider other)

{

SetCollidingObject(other);

}


public void OnTriggerExit(Collider other)

{

collidingObject = null;

}

We then call our SetCollidingObject method. So why are we calling it twice? This is purely to prevent bugs. If we touch something, we want Unity to know that were touching that object. But at the same time, if we keep touching an object, we don't want Unity to forget that we can still pick that object as long as we are in contact with it. Once we stop touching the other object, we don't want to be able to pick up the object anymore, so we remove the stored reference of the colliding object.

Finally, lets add in the grab functionality itself now. Add the following code immediately after the previous section code. Let's start out with the AddFixedJoint() function. Basically we are creating a FixedJoint game object, which will connect our grabbing controller rigidbody with the object we are grabbing.

The breakforce and breakTorque numbers are somewhat arbitrary, but we want to make the joints relatively strong, so that our grip on objects doesn't unexpectedly break.

private FixedJoint AddFixedJoint()

{

FixedJoint fj = gameObject.AddComponent<FixedJoint>();

fj.breakForce = 65536;

fj.breakTorque = 65536;

return fj;

}

So what's happening in the grab function? We want to take the object we're touching and we no longer want to simply "collide" with the object, we want to physically hold it. So we now store the colliding object in "held", and assign the collidingObject variable to null. We then connect the controller rigidbody to the held object's rigidbody with a FixedJoint object.

private void Grab()

{

held = collidingObject;

collidingObject = null;


var joint = AddFixedJoint();

joint.connectedBody = held.GetComponent<Rigidbody>();

}

If we can pick something up, we should also be able to drop it right? First, we check to see if a FixedJoint exists. This means that we are, in fact, grabbing something. The next two lines of code serve to erase the FixedJoint from existence.

Then we maintain the controller's velocity and angular velocity before releasing the object. This allows us throw our ball that we made way back when, instead of the ball just dropping straight to the ground. We do this by accessing the SteamVR_Behavior_Pose script attached to the controller, which has a function that gets its current velocity and angular velocity

Lastly, set the held variable to null to show that we no longer are holding the object.

private void Drop()

{

if (GetComponent<FixedJoint>())

{

GetComponent<FixedJoint>().connectedBody = null;

Destroy(GetComponent<FixedJoint>());


held.GetComponent<Rigidbody>().velocity = GetComponent<SteamVR_Behaviour_Pose>().GetVelocity();

held.GetComponent<Rigidbody>().angularVelocity = GetComponent<SteamVR_Behaviour_Pose>().GetAngularVelocity();

}


held = null;

}

Now lets put our grab and drop functions into practice. Add the following code to the Update() function. Since we made our Grab and Drop functions, this makes our Update function look so much cleaner than it could have been. To summarize, whats happening is; If we press the trigger on a controller, and the controller is colliding with an interactable object, then grab the object. Then, if we lift the trigger on the controller, if the controller was holding an object, then execute the drop function. By putting this in the Update, we are telling Unity to execute this code every frame. In other words, on a Vive, that would mean this function checks for grabbing and dropping 90 times every second.

// Update is called once per frame

void Update()

{

if (grabPinch != null)

{

if (grabPinch.GetStateDown(inputSource))

{

if (collidingObject)

{

Grab();

}

}

if (grabPinch.GetStateUp(inputSource))

{

if (held)

{

Drop();

}

}

}

}

Save the file, and build it (Should be Ctrl/Cmd + Shift + B). Building will allow us to check to see if maybe we made any sort of mistakes in our code. If the build succeeds and there are no errors, we should be good to go back to Unity. As soon as we save the code, it should also be saved in the Unity Project itself.

Let's add another simple script that will make the program a lot easier during runtime. Create a new script and call it BallController.

public class BallController : MonoBehaviour {


private Vector3 position;


// Use this for initialization

void Start () {

position = transform.position;

}

// Update is called once per frame

void Update () {

if (transform.position.y < 0)

{

var rb = GetComponent<Rigidbody>();

transform.position = position;

rb.velocity = Vector3.zero;

rb.angularVelocity = Vector3.zero;

}

}

}

Since our floor is floating in the middle of nowhere, if we throw a ball and it falls off the edge, then we'll have nothing to throw anymore. This script here basically just checks to see if the ball is below the floor. If it is, then return it to its original position.

The notable syntax here includes:

  • The Vector3 object: Vector3 is a 3-D vector that stores an x, y, and z. This is useful for storing position and movement in a 3-D scene.

  • transform stores everything in the "Transform" field that we saw earlier in the object inspector. Things like scale, position, etc.

  • Lastly, something cool about Unity is that there are a lot of shortcuts that simplify the code we have to write. An example of this would be Vector3.zero. Normally, we would have to write something to the effect of "new Vector3(0, 0, 0), but instead, we can simplify a little to something a little more intuitive.

Now all we need to do is take the scripts, and attach them to their related objects. To do this, all we need to do is select both controllers and drag the "ControllerGrab" script into their inspectors, and drag "BallController" to the Ball's inspector.

When you drag the "ControllerGrab" script, you may notice two dropdown menus in the component. Something really cool about Unity is that you can set public variables to GameObjects and components from the Unity editor. In this case, make sure that the "Grab Pinch" variable is set to "\actions\default\in\GrabPinch," which will associate the "action" from the SteamVR Input to the "action" in our script. Also, make sure that the input source for the left controller is "left hand" and for the right controller "right hand".

This section is not necessary to the tutorial, so if we are out of time, skip this section.

Having only one ball and once target to knock over seems kind of boring, doesn't it? Let's add some more of each!

Create a new folder in the assets folder called "Prefabs". Now drag "Target" and "Ball" from the hierarchy into the Prefabs folder. Delete them from the Hierarchy Menu.

Prefabs are basically blueprints for objects we make. If we make a Prefab, then we can make multiple copies of that object easily, and if we change one Prefab object, we have the option to change all others made from the same prefab in the same way.

Create two new Game Objects in the Hierarchy Menu (Create>Create Empty) (Be sure that their Transform properties are zero'ed out). Name one "Balls", and the other "Targets". Now drag the ball prefab to "Balls" and the "Target" prefab to Targets (This make the hierarchy menu a little more organized). Now right click the Ball (or Target), and select "duplicate". Now if we select the duplicate in the hierarchy menu and focus on it with "f", we should notice a colored cube with arrows. If we click the bottom face with the cube, you'll notice that we can drag the object around easily without having to manipulate any numbers. This way, we can duplicate a bunch more targets and balls to throw at them.

Once we've finished this, our project is now ready to go! Only one more step left. Go to the build settings with (File>Build Settings) or with (Ctrl/Cmd +Shift+B).

Make sure that the platform selected is "PC, Mac & Linux Standalone", then click Build. As for where to save this build, I would recommend navigating to the project directory folder (Where the .vs, Assets, Library, etc. folders are), creating a new folder called Builds, and saving the build there.

Once that's finished, all you need to do is find a Vive (Try the VR Lab), and enter into a whole new reality!