Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We're going to write a script that allows us to pick up the basketball and throw it. This sounds simple when stated in a sentence, but there are several steps involved in order to create this intuitive interaction.
Resources
- Room-Scale VR - Project Files - This is a zip file that contains the Unity project for this course. Download the file, unzip it, and then open the project in Unity.
-
0:00
[MUSIC]
-
0:04
We're going to write a script that allows us to pick up the basketball and throw it.
-
0:10
When it's stated in a sentence like that, it sounds like a pretty simple and
-
0:14
intuitive task, however we have to write code that works every step of the way.
-
0:21
The script will be attached to each controller and
-
0:25
needs to first detect if the controller is intersecting with an object.
-
0:30
Then if the controllers intersecting it also needs to detect if the player
-
0:35
is holding down the trigger button on the controller to pick up the object.
-
0:40
If the controller is intersecting an object and the player is
-
0:44
holding down the trigger, then we can attach the object to the controller.
-
0:51
Once we've attached the object to the controller,
-
0:54
we need to be able to release the object.
-
0:57
Like I said, it's a little bit more complicated than it sounds, so
-
1:01
let's get started by creating the script and detecting intersections.
-
1:07
In the project window, navigate to the scripts folder and create a new script.
-
1:18
And call it controller interaction.
-
1:25
Then in the higher key select, the left and
-
1:29
right controller and then drag the controller interaction
-
1:35
script from the project window to the inspector.
-
1:39
Then open the script for editing.
-
1:42
First delete these start and update methods as we will not need them.
-
1:48
Next we need to get the controllers Steam VR tracked object component and
-
1:53
figure out which controller the script is actually attached to.
-
1:58
To kick that off, let's create some of the variables we're going to need.
-
2:03
I'll type this out and then explain it, so
-
2:07
inside the class here we'll create some variables.
-
2:11
First will be a private steam VR _TrackedObject.
-
2:17
And we'll just call that lowercase tracts a case Object.
-
2:26
And then we'll create a private SteamVR_Controller.Device, and
-
2:31
we will call this device and
-
2:37
we'll create a private rigid body and call this attach point.
-
2:46
And then finally, we'll create a private FixedJoint
-
2:52
and call that variable, fixedJoint.
-
2:57
The SteamVR TrackedObject class is part
-
3:01
of the SteamVR plugin and it contains an index will need
-
3:06
to determine which object in the tracking volume is being used.
-
3:11
The SteamVR Controller.Device is an object to which will
-
3:18
assign the controller once we've used the index to determine which one we're using.
-
3:24
That's a little confusing but
-
3:26
it will make more sense when we write the rest of the script.
-
3:30
The attach point is a rigid body and
-
3:34
we'll use this fixed joint object to connect the basketball to the controller.
-
3:41
Again, this will make more sense when we actually use these variables.
-
3:47
Now let's use the Awake method to get some of these components and
-
3:52
assign them to our variables.
-
3:54
It's all create some space to work here and then I'll type, void Awake.
-
4:05
And then, first, we need to get the controller components.
-
4:12
So let's do that.
-
4:13
Tract object.
-
4:17
It's going to be equal to get components,
-
4:23
Steam VR TrackedObject.
-
4:30
So that component and
-
4:32
then the attach point is going to be the rigidBody components so let's get that.
-
4:39
So we'll get the rigidBody component that's attached to this game object.
-
4:48
Using awake instead of star in this case isn't totally necessary but awake executes
-
4:54
earlier than start which can be helpful if this project becomes more complex and
-
5:00
needs these components to be accessible by other scripts in their start method.
-
5:05
Again, not the case here but it's good to think ahead.
-
5:09
That's most of the set up will need to get going.
-
5:12
However, without any of these variables wee can
-
5:15
actually detect intersections right away.
-
5:18
Using the on trigger stay message.
-
5:22
So let's type that out.
-
5:26
Just below awake here,
-
5:29
we'll type void OnTriggerStay and
-
5:34
inside we'll pass, Collider other.
-
5:42
And this will execute any time this trigger
-
5:45
Collider intersects with another Collider.
-
5:50
But how will we know this is working correctly?
-
5:53
We could log output to the console but
-
5:56
we won't be able to see it until we take off the VR headset and go back to unity.
-
6:01
It would be a lot more fun to get some visual feedback in the game.
-
6:05
So let's grab the collider named Other.
-
6:10
Get its game object and set its material color to a totally random color.
-
6:16
So inside here I'll type other to get the other Collider.
-
6:21
Take game object to get the game object that the other Collider is attached to or
-
6:27
use gets component.Renderer to get
-
6:34
the render that's attached to this game object, so this is usually a mesh render
-
6:41
and then from the mesh render we can get its assigned material and
-
6:45
on the material, we can get its materials color.
-
6:50
And this is what we want to set to a random color value
-
6:54
any time the controller intersects with the object.
-
6:59
So it will create a new color here.
-
7:04
And we're going to set its R.G.B. values.
-
7:10
Randomly so will type Random.Range for
-
7:15
the red value and this needs to be a value between zero and one.
-
7:21
So we'll just pick a random float value between zero and one.
-
7:26
And then we just need to do this for green and blue.
-
7:30
So I'll just paste this two more times here separating with comments.
-
7:35
And then I'll break this out into separate lines just so it's easier to read here.
-
7:41
So let's do that, And then I'll format this so
-
7:47
that we can see what's going on.
-
7:53
There we go.
-
7:54
So what are we doing here, we're taking the Collider other, getting its game
-
7:59
object, getting backgame objects renderer, getting that renders material,
-
8:04
getting that material's color, and then we're setting that color for
-
8:08
this game object to A random color and
-
8:12
we're doing this every time on trigger stay is called which will be constantly.
-
8:18
So the object that gets intersected will just flash to random colors.
-
8:25
In production, you wouldn't ever want to use the GetComponent method like
-
8:29
this because it's a fairly expensive line of code and
-
8:34
in this case, it's being called many times per second.
-
8:38
We'll take this code out when we're done with it but for
-
8:40
testing purposes, this will give us some nice visual feedback, save the script.
-
8:46
Return to unity.
-
8:48
Save the scene.
-
8:50
Hit the play button and then jump and in the V.R..
-
8:55
So now when you load up the game.
-
8:58
You should see your controllers and
-
9:01
you should also see the basketball sitting here on the court.
-
9:06
And you may have noticed that your basketball may have already changed colors
-
9:10
and it's also possible that the floor of your court has changed colors as well.
-
9:15
And this just depends on how the controllers were first initialized
-
9:20
when the tracking volume was started at the start of the game
-
9:24
because the controllers will change the color of any object they intersect and
-
9:28
it's possible that the controllers may have already intersected the floor or
-
9:33
may have already intersected the basketball.
-
9:35
So let's see if this works and
-
9:40
it looks like it does when we intersect the controller.
-
9:43
With the basketball, will randomly change colors and
-
9:49
when we remove the controller from the basketball,
-
9:52
it will stick on whatever the last randomly selected color might have been.
-
9:58
So now that we know this is working
-
10:01
in that we can detect these intersections we're ready to move on.
You need to sign up for Treehouse in order to download course files.
Sign up