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 able to pick up objects, but once we do, they're stuck to the controller. Let's write some code that will allow us to release the ball after it's been picked up.
Unity Documentation
- DestroyImmediate() - This section of the Unity Script API documents the DestroyImmediate() method, which will destroy an object without waiting for the end of the frame.
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
We're able to pick up objects but once we do, they're stuck to the controller.
-
0:05
Let's raise some code that will allow us to release the ball
-
0:09
after it's been picked up.
-
0:11
So let's switch back to the controller interaction script and,
-
0:17
in here, we need to release objects.
-
0:21
The player will be able to release objects by simply releasing the trigger
-
0:25
button on the controller after they've press down to pick up an object.
-
0:30
The best place to check for this is inside the fixed update method because,
-
0:36
at any moment, the player could release the trigger button so
-
0:40
we need to check every physics update.
-
0:44
In addition, they need to be holding an object in the first place in order for
-
0:48
the object to be released.
-
0:50
We can test for that by checking whether or not the fixed joint variable is null.
-
0:56
So, let's write that out.
-
0:58
So, just after we've done the device assignment,
-
1:04
we'll type another big comment here to just describe this general area of code.
-
1:09
We'll say here is where we are going to release objects.
-
1:16
So again, if there's a fixed joint attached and
-
1:24
the trigger button is deactivated.
-
1:32
So we'll type that out in code if (fixedJoint),
-
1:38
lowercase F, uppercase J, is not equal to null,
-
1:45
which means it is set and device.GetTouchUp,
-
1:51
meaning that the button is released.
-
1:58
So we want that button to be SteamVR_Controller.Button.Mask.Trigger.
-
2:06
So if both of these are true, then we will execute some code here.
-
2:12
Great, now, how should we release the object?
-
2:16
Well, in order to play basketball or throw any object,
-
2:21
you need to be able to transfer your energy into the object.
-
2:26
Unity doesn't have the physics concept of kinetic energy but
-
2:31
it does have velocity and angular velocity.
-
2:35
The velocity describes the speed of an object's translation in a given direction
-
2:41
and angular velocity is the speed and direction of an object's rotation.
-
2:47
We'll need to assign that information to the ball through its rigid body so
-
2:52
let's grab that first.
-
2:53
[SOUND] First, get the rigid
-
2:59
body on the fixed joint's gameobject.
-
3:06
We'll say Rigidbody and we'll just create a new rigid body here.
-
3:13
[SOUND]
-
3:14
fixedJoint.gameObject.GetComponent<
-
3:23
Rigidbody>, and
-
3:27
that will assign it.
-
3:31
Now, we want to detach the ball from the controller.
-
3:36
We can do that by destroying the fixed joint component.
-
3:40
This will also set the fixed joint variable to null.
-
3:46
That way, the if statement to pick up objects in the OnTriggerStay
-
3:51
method will be able to execute again the next time the player picks up the ball
-
3:55
because it checks if the fixed joint variable is null.
-
3:59
So, let's type that out and then I’ll explain it a bit more.
-
4:04
So just after this line.
-
4:08
I want to destroy the fixed joint.
-
4:14
And we're going to say DestroyImmediate(fixedJoint).
-
4:23
So in this line, we're destroying the fixed joint component
-
4:28
by calling destroy immediate on the fixed joint variable.
-
4:31
But instead of using the destroy method like we have in previous courses,
-
4:37
we're using the DestroyImmediate method.
-
4:40
DestroyImmediate will nullify the component when this line of code
-
4:46
is executed and that's necessary because we're going to set the velocity next.
-
4:51
If we just used destroy, like normal, the fixed joint wouldn't be destroyed until
-
4:56
the end of the frame and the velocity won't be transferred because the game
-
5:01
object is still being controlled by the fixed joint rather than the rigidBody.
-
5:06
The Unity documentation discourages using DestroyImmediate in
-
5:11
most cases but, in this code, we do need the component to be destroyed right away.
-
5:17
For a link to the Unity documentation on DestroyImmediate,
-
5:20
see the notes associated with this video.
-
5:23
So finally, let's transfer the velocity to the rigid body.
-
5:27
I'll type this out and then explain it.
-
5:30
So I'll just scroll down a little bit here and,
-
5:36
just after DestroyImmediate,
-
5:41
I'm going to set the Rigidbody's velocity and
-
5:48
the angular velocity to the controller's values.
-
5:55
So we'll take that Rigid body that we just created and
-
6:02
grabbed from the fixed joint's game object and
-
6:08
we'll say rigidBody.velocity is equal to the device.velocity and
-
6:16
then rigidBody.angularVelocity is
-
6:21
equal to the device.angularVelocity.
-
6:28
So in this code, we're taking the velocity and angular velocity
-
6:33
from the device class and the device variable,
-
6:38
and we're applying it to the objects rigidBody.
-
6:43
This will make the basketball feel like it's moving and
-
6:46
spinning at an appropriate speed when we release the ball, enabling
-
6:51
us to throw the ball and even dribble it in the way that you would expect.
-
6:56
So save the script, return to Unity and
-
7:00
then play the game, and I will see you in VR.
-
7:05
So now if we pick up the ball, we can release it and
-
7:09
it will fall to the ground, and we can pick it back up.
-
7:14
However, if we throw it outside the tracking
-
7:17
volume, we don't have any way to retrieve it.
-
7:22
We also don't have any way to interact with the scoreboard or start the game.
-
7:27
In the next video, we'll fix these problems.
You need to sign up for Treehouse in order to download course files.
Sign up