Home and Learn: Games Programming Course


VR Grabbables in Unity

In previous lessons, you have added a scene to Unity. You have added an XR Rig so that you can walk around your scene with your Vr headset on. And you also learned how to add a pair of hands and how to animate them. In this lesson, you'll learn how to grab things with those animated hands.

 

You may well have tried to pick up the gun or shield. If you did, you would have been disappointed. That's because anything you want to pick up in your game needs a grabbable script added to it.

Anything that can be picked up in your game needs a script called XR Grab Interactable added to it; any part of a character that is going to pick up a game object needs a script called XR Direct Interactor added to it.

Let's start with our hands, which are going to do the picking up, so they both need a Direct Interactor adding to them. First, though, we need to remove a few components.

In the Hierarchy on the left, select both of your hand controllers: Click on the Left Controller, hold down the CTRL key on your keyboard, and then click the Right Controller.)

Unity Hierarchy showing two game objects selected.

In the Inspector on the right, notice that we have 5 components:

Unity Inspector showing 5 components added to a game object.

We want to add a Direct Interactor component. However, you can't have two Interactors on the same game object. If we tried to add a Direct Interactor, Unity would complain that we already have an Interactor, which is the XR Ray Interactor. So we need to remove it first.

To remove the XR Ray Interactor, click the three dots on the right. From the menu that appears, select Remove Component:

Arrow showing how to remove a component from a game object in the Unity Inspector.

We don't need three of the others, now that we've removed the XR Ray Interactor. Remove these, and in this order:

XR Interactor Line Visual
Line Renderer
Sorting Group

You should be left with just a Transform component and XR Controller (Action Based) component.

To add the XR Direct Interactor, click the Add Component button. Type Direct into the search box. Select the XR Direct Interactor from the list:

Unity Inspector showing the XR Direct Interactor script being selected.

The Inspector should now look like this:

Unity Inspector showing the XR Direct Interactor script

Notice where the red arrow is pointing in the image above - to the circle to the right of the Interaction Manager. Click the circle and select XR Interaction Manager from the box that appears:

The XR Interaction Manager dialog box in Unity.

The Inspector will then look like this:

Unity Inspector showing a XR Direct Interactor added to a game object.

You can leave the other settings on their defaults.

As well as a Direct Interactor, we need a Sphere Collider on both of our hands, otherwise the hands will just pass through the game objects. Click the Add Component button again. This time, type sphere in the search box:

Unity Inspector showing a Sphere Collider game object being selected.

When the Sphere Collider is added, change the radius to 0.1, as it's a bit big, at the moment. Also, put a check in the box for Is Trigger:

Unity Inspector showing a Sphere Collider with its radius changed and Is Trigger set to True.

Your scene should look something like this:

Unity game scene showing a sphere collider highlighted.

The Sphere Collider shows up as a green ball.

Now that we have sphere colliders and Interactors on both hands, we can set up the game objects to be grabbable. Let's start with the rifle.

Select your rifle in the Hierarchy on the left.

Game Objects that can be picked up in VR need a XR Grabble Interactable on them. So click the Add Component button again in the Inspector on the right. Type grab in the search box. Select XR Grabble Interactable from the list:

Unity Inspector showing a XR Grabble Interactable script being selected.

Notice that when you add an XR Grab Interactable, Unity also adds a Rigidbody component. Objects need a Rigidbody so they can be picked up.

Leave the Rigidbody settings on their defaults.

There's quite a lot of settings on the XR grab Interactable, though:

Unity Inspector showing a XR Grabble Interactable script with all its settings.

Just like before, add your Interaction Manager from the list (indicated by the red arrow below). The two red rectangles are settings we'll come back to.

We'll have to fix the rifle before testing it out, otherwise you'll get errors about its mesh.

In the Hierarchy on the left, expand the rifle item. Select the two items underneath it:

Unity Hierarchy showing two game objects selected.

In the Inspector on the right, check the box for Convex:

OK. Time to test it out. Put your VR headset on. Run your game. Now walk up to the table and grab the rifle by holding down a grip on your controllers. You should see something like this:

The problem is that the hand is not grabbing the rifle in the correct place. To remedy that, you can add attach points to the rifle. You can then grab the rifle at the attach point. Let's see how to do that.

 

VR Attach Points in Unity

It's easy enough to add an attach point. It's not so easy getting the attach point in the correct position. But let's give it a go.

The idea is to add an empty game object as the attach point. So, with your rifle selected (the parent, not any of the two children), right click. From the menu that appears, select Create Empty. Name the empty game object RifleAttachPoint:

Unity Hierarchy showing an empty game object selected.

Now have a look at the Scene:

Unity game scene showing a rifle and its attach point.

You can see the 3D cube, which is the RifleAttachPoint. You can drag the arrows about to move the attach point to where you want it. This will be the new point at which our hand model will grab the rifle.

However, you need to tell the parent object about the attach point. So, click on your rifle object in the Hierarchy. In the Inspector on the right, either drag and drop your RifleAttachPoint onto the Attach Transform slot in the XR Grab Interactable script. Or you can select it from the popup box indicated by the red arrow below:

Unity Inspector showing where to add the attach point game object.

Unity Inspector showing a popup box with a Transform selected.

Once you have your attach point nicely positioned in the Scene, put your headset on and try it out. Grab the rifle and see if you're grabbing at a decent position.

However. with this particular gun, and these hands, you're not going to get it into a perfect position. Here's a short video of ours. Notice what happens at the end - we have a problem to fix!

The problem is that gun goes through the table (and any other game object). To remedy this, there is a setting on the XR Grab Interactable script you can change.

With the rifle selected on the Hierarchy, locate the Movement Type property in the Inspector on the right:

Unity Inspector showing the Movement Type property highlighted.

Click the dropdown to reveal a list of three options:

Unity context menu showing three options.

The three options are Velocity Tracking, Kinematic, Instantaneous. The default is Instantaneous, which is causing our problems. Here's an explanation of all three:

Velocity Tracking
Uses the Physics engine to move a game object. Can cause delays but looks better for things like guns and doors - if you need Physics on your grabbables, this is the one to use.

Kinematic
Passes through objects that don't have a Rigidbody attached. Collides with objects that do have a Rigidbody. Applies physics to objects it collides with.

Instantaneous
Use the Transform of a game object to move rather than the Physics engine. A lot smoother but passes through game objects.

 

Change your Movement Type to Velocity Tracking. You should find that gun no longer passes through the table.

 

Exercise
Add XR Grab Interactable scripts to your other table objects. Use attach points to improve the grab positions of your objects. (Don't forget to check the Convex box on any Mesh Colliders, otherwise you'll get red errors.) In the video below, we've grabbed the rifle and the shield. Then we've started shooting:

 

 

Wait, what? Shooting? You didn't teach us that! OK, although we've done gun firing in another tutorial, we'll go through it here for VR. But the process is more or less the same. We'll do it in the next lesson below.

<--Back to the Unity 3D Course Contents Page

 


Email us: enquiry at homeandlearn.co.uk