Advanced VR Interactions in Unity Tutorial

Create advanced interactions and mechanics in your Unity VR app, including working with tooltips, joints, snapping and haptic feedback! By Eric Van de Kerckhove.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Auto Grabbing

To automatically grab an object at the start of the scene, you could write a script, but as it turns out, VRTK has a handy component you can add to a controller alias called VRTK_Object Auto Grab. This can automatically grab any interactable objects from the scene or an instantiated prefab.

Select the Right Controller under [VRTK_SDK_MANAGER] \ [VRTK_SETUP], and add a VRTK_Object Auto Grab component to it.

Then, drag the Pistol to the Object To Grab field.

That’s it! Try the scene again, and the pistol will automagically replace your right controller.

Hand Attachments with Joints

There are other methods of attaching objects to controllers besides just parenting them.

If you played around in the scene a bit, you might have noticed you can move the objects right through other objects as soon as you’re holding them. It’s almost as if you’re holding a ghost object. Spooky!

This is because the rigidbody gets set to kinematic if you’re using the VRTK_Child Of Controller Grab Attach component.

For the next object, you’ll use a fixed joint to attach the object to the controller, giving it some realistic behavior.

Create a new empty GameObject as a child of RW Props. Name it VikingDoll, and set its Position and Rotation to (X:8, Y:1.04, Z:5.5) and (X:0, Y:-90, Z:0) respectively.

Drag the Viking model from the Razeware\Models folder onto VikingDoll in the Hierarchy to parent an instance of the model, and set its Scale to (X:0.16, Y:0.16, Z:0.16).

Now, to add a basic VRTK setup.

Select VikingDoll and open the VRTK setup wizard (Window ▸ VRTK ▸ Setup Interactable Object).

Leave everything at their default values except for Grab Attach Mechanic; set this to Fixed Joint and hit the Setup selected object(s) button.

Close the window and remove the VRTK_Interact Haptics component and enable Precision Grab on the VRTK_Fixed Joint Grab Attach component.

Add a Box Collider to the VikingDoll, and set the Center to (X:0, Y:0.2, Z:0.02) and the Size to (X:0.2, Y:0.4, Z:0.18).

That’s all you have to change to get fixed joint attaching working!

The precision grab allows you to grab the object at the particular point where the controller is colliding with the object.

Try the scene again and grab the viking doll. If you attempt to move the doll through the table, you’ll “lose grip”, and you’ll drop the doll.

The joint breaks because the force between the controller and the doll is too great. The controller can move around freely, but the doll can’t enter solid objects, so it’s as if you pull a string from both ends until it snaps.

The force needed to snap this imaginary string is set by the Break Force field on the VRTK_Fixed Joint Grab Attach component.

If you don’t want to break the link between the controller and the object at all, but still want to avoid objects going through each other, there’s also the Track Object grab attach mechanic.

This attachment method applies the velocity of the controller to the grabbed object. It’s less accurate than the methods previously discussed, but it may help you in certain scenarios.

Object Snapping

You can create a lot of different behaviors by snapping objects together. Snapping objects together means that you can insert one object into another object. For example, a USB stick in a USB port, a wheel on a car, and so on.

In this section, you’ll create a battery and a battery holder, which provides the museum with some extra juice to power an extra set of lights.

First, create the battery:

Add a new empty GameObject to the scene, name it Battery, and set its Position and Rotation to (X:8, Y:1.08, Z:8) and (X:0, Y:-120, Z:0) respectively.

Drag the Battery model from the Razeware\Models folder onto the GameObject you just created, so it has some visuals.

The Battery model is composed of two parts: the battery and the battery holder:

Separate BatteryHolder by dragging it between Object Tooltips and Battery in the Hierarchy. You’ll be asked to break the prefab instance. When prompted, select OK.

To make things less confusing moving on, rename it to BatteryHolderModel, and reset its Position to (X:0, Y:0, Z:0) so it’s out of the way. To add basic functionality to the battery, select the Battery parent GameObject and open the Interactable Object wizard.

Set the Touch Highlight Color to a solid yellow (R:255, G:255, B:0), uncheck Add Haptics, and click the Setup selected object(s) button.

Add a Capsule Collider component to the Battery. Set its Radius to 0.08, its Height to 0.4, and enable Is Trigger. Also, set the Direction to X-Axis.

Finally, drag the parent Battery GameObject to the Razeware\Prefabs\Grab folder to turn it into a prefab.

You can now pick up the battery and throw it around, but there’s still nowhere to plug it in. This is where the BatteryHolder comes into play!

Create a new empty GameObject, name it BatteryHolder, and set its Position to (X:0, Y:1.35, Z:9.2).

Drag BatteryHolderModel onto BatteryHolder to parent it, and set its Position and Rotation to (X:0, Y:0, Z:0) and (X:-90, Y:180, Z:0) respectively. The result will look like this:

To allow objects to accept other objects to snap onto them, you need a Snap Drop Zone — this component filters out the objects that can be attached to it and handles the movement, rotation and scale transition.

Add a VRTK_Snap Drop Zone component to the BatteryHolder. Assign the Battery prefab to the Highlight Object Prefab field, and set Snap Duration to 0.2. Set the Highlight Color to a bright green color (X:40, Y:255, Z:0).

You’ll now see a pink transparent copy of the battery inside the holder:

Right now, most of the snap drop zone settings are ready, except it won’t accept any objects as-is. To add valid objects, you need to add a Policy List; this is a handy way of filtering based on several requirements. It can filter by tag, script or layer, and can even accept or reject everything if you wish.

Add a VRTK_Policy List component to the BatteryHolder, set its Operation to Include, and fill in Battery in Element 0 of the list.

Now all of the objects with the Battery tag are allowed to snap into this object. Assign the Battery tag to the Battery by selecting the Battery GameObject and choosing Battery from the Tag dropdown. Click the Apply button afterward, so the prefab gets saved too.

Select BatteryHolder and drag the VRTK_Policy List component onto the VRTK_Snap Drop Zone component’s Valid Object List Policy field to set up the reference to the policy list that should be used for objects allowed to be snapped into place on the battery holder.

The drop zone needs a collider and a rigidbody so it can detect when an object enters in range. Select BatteryHolder and add a Rigidbody and a Capsule Collider component. Disable the gravity on the rigidbody and make it kinematic: you don’t want it falling on the floor.

Set the Radius on the collider to 0.1, set Height to 0.5, and change the Direction to X-Axis.

There’s one final part missing to complete the BatteryHolder: you need a way of making stuff happen when the battery is put inside the batter holder. For this, you’ll need another component: a Snap Drop Zone Unity Events. This lets you visually link the events.

Add a VRTK_Snap Drop Zone_Unity Events component to BatteryHolder, and add a new event to both On Object Snapped To Drop Zone and On Object Unsnapped From Drop Zone. Drag ExtraLights to both Object slots and select GameObject ▸ SetActive. For the snap event, check the box so it’ll become active.

Create a new folder in the Razeware\Prefabs folder named DropZone. Drag the BatteryHolder to the newly created folder to turn BatteryHolder into a prefab.

Everything is ready for snapping now. Run the scene and try to pick up the battery; teleport to the battery holder and snap the battery into place.

If everything works correctly, you’ll see an extra set of lights switch on.