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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Advanced VR Interactions in Unity Tutorial
35 mins
Controller Tooltips
When you start playing a new game, learning the controls can get confusing, especially with VR — a developing platform that doesn’t yet have a lot of standard controls schemes like PCs and consoles.
In this section, you’ll create a set of tooltips like this one:
Creating these is ridiculously simple, all you need to do is drag a ControllerTooltips prefab from the VRTK\Prefabs folder onto Left Controller.
This adds a bunch of pre-configured tooltips — like you added in the previous section — and the VRTK_Controller Tooltips component acts as a manager for these.
Look at the Button Text Settings on the newly added ControllerTooltips. These correspond to all possible buttons on the HTC Vive and Oculus Rift. The names don’t necessarily match that of the controller, however, here’s an overview of the HTC Vive controller and an Oculus Touch controller:
- Trigger: Hair Trigger on Vive, Trigger on Rift
- Grip: Grip on Vive, Grip on Rift
- Touchpad: Touchpad on Vive, Analog Stick on Rift
- Button One: N/A on Vive, Bottom Button on Rift
- Button Two: Menu Button on Vive, Top Button on Rift
- Start Menu: N/A on Vive, Start Button on Rift
Notice the last three differ between the Vive and the Rift. In this case, you’ll be targeting the HTC Vive setup as an example. If you’re using an Oculus Rift, switch around the Button Two and Start Menu Text.
Change the Button Text Settings to the following:
- Trigger Text: Teleport Pointer
- Grip Text: Grab
- Touchpad Text: Shoot
- Button One Text: (empty)
- Button Two Text: Menu
- Start Menu Text: (empty)
The Object Tooltips are hidden for all empty text fields. That means, if you only want to label just one or two inputs, that’s not a problem!
Now that tooltips are done, save the scene so you don’t lose any work. Then, run the scene, look at your controller, and you’ll see the tooltips spread all around it.
Adding tooltips was fun, right?
In the next sections, you’ll explore how to add some specific functionality to pick up objects.
Fixing Objects to your Hands
In some cases, you might want an object to be fixed to a controller. Or you might want to replace an object with another one after you grab it. Think guns, swords and boxing gloves, for example. You can even automatically grab a GameObject or cloned prefab right at the start of the scene.
For now, though, you’ll start off with creating a basic example: a tank figurine that attaches to your controller permanently once you grab it. This is what happens when you spill a bottle of glue on your tank!
Simple Fixing
Create a new empty GameObject, name it TankFigure, and set its Position to (X:8, Y:1.115, Z:3.5). Then, drag Tank from the Razeware\Models folder and drop it onto TankFigure, making Tank a child of TankFigure.
Change the newly added model’s Scale to (X:0.21, Y:0.21, Z:0.21), and drag TankFigure onto RW Props to parent it.
When you’re done, the new hierarchy will look like this:
Create a new folder inside the Razeware\Prefabs folder and name it Grab. Drag the TankFigure onto the new folder to turn it into a prefab.
You’ll now have a little tank sitting on the table.
This is cool, but you need a way to interact with it.
The easiest and fastest way to add the components you’ll need for this interaction is to use VRTK’s interactable setup wizard.
With TankFigure still selected, open the setup by selecting Window ▸ VRTK ▸ Setup Interactable Object in the top menu.
Leave everything at their default values and press the Setup selected object(s) button at the bottom.
Close the setup window and remove the VRTK_Interact Haptics component from TankFigure.
Under the Grab Options in the VRTK_Interactable Object, set Valid Drop to No Drop; this setting makes it so the player can’t drop the tank after grabbing it.
Before testing if it all works, add a Box Collider component to TankFigure, set its Center to (X:0, Y:0.06, Z:0) and its Size to (X:0.3, Y:0.23, Z:0.3).
This surrounds the small tank. To test it out, dive into VR and try to pick up the sticky tank.
No matter how hard to try, this tank won’t come off! If you don’t want your players to drop something important, or if you want to attach silly stuff to their controllers, this works out nicely.
Next, you’re going to add a pistol that replaces the controller.
Custom Fixing
Add a new empty GameObject to the scene as a parent of RW Props, and name it Pistol. Change its Position to (X:8, Y:1.15, Z:4.5), and drag the Pistol model from the Razeware \ Models folder onto Pistol to create a child.
At the moment, the pistol is a tad too big and not positioned correctly. To fix this, change its Position to (X:0, Y:0, Z:0), its Rotation to (X:-90, Y:90, Z:-90), and its Scale to (X:23, Y:23, Z:23).
There, all better:
Select the Pistol parent and open the VRTK setup wizard (Window ▸ VRTK ▸ Setup Interactable Object). Leave everything at their default values and hit the Setup selected object(s) button.
Close the window and remove the VRTK_Interact Haptics component from Pistol.
Add a Box Collider and set its Center and Size to (X:0, Y:-0.018, Z:0.065) and (X:0.11, Y:0.24, Z:0.37) respectively.
Change Valid Drop on the VRTK_Interactable Object component to No Drop. Right now, it behaves exactly like the tank. To hide the controller when the pistol is grabbed, add a VRTK_Interact Controller Appearance component to the Pistol, and enable the Hide Controller On Grab checkbox.
Test out the scene and see if the controller disappears when you grab the pistol. Well, the controller disappears, but you’re stuck with a gun that’s at an awkward angle:
The reason for this is because GameObjects are attached to your controller at the angle you started grabbing them. To fix this, you’ll need to add a snap handle so the position and rotation of the pistol are always the same relative to the controller.
To create the snap handle, create a new empty GameObject as a child of the parent Pistol GameObject and name it SnapHandle.
Set its Position to (X:0, Y:0.015, Z:0.015) and its Rotation to (X:75, Y:0, Z:0); this will orient the handle like this:
Notice how the handle is rotated 75 degrees on the x-axis? This is important to remember if you want to position snap handles correctly for objects with a grip like guns, swords and other objects.
Here’s how the snap handle will affect how the controller grabs the pistol:
Now drag the SnapHandle to the Right and Left Snap Handle slots in Pistol’s VRTK_Child Of Controller Grab Attach component.
Make the Pistol into a prefab by dragging it into the Razeware\Prefabs\Grab folder, then play the scene again.
The pistol is now aligned to your hand in a more natural position and will follow your hand rotation. The controller itself is also hidden.
This what the developers of Job Simulator have dubbed Tomato presence:
Before moving on to the next section, there’s one more related mechanic you need to work out: automatically grabbing a GameObject.