Advanced VR Interactions in Unity Tutorial
This is an abridged version of a chapter from our new book: Unity AR & VR by Tutorials. Enjoy!
If you’ve already worked with VRTK a bit, and you feel that you’ve mastered the basics of implementing gameplay in virtual reality using VRTK, you’re ready to dive into some more exciting ways to interact with your virtual worlds.
In this tutorial, you’ll learn how to:
- Create tooltips for objects and controllers.
- Fix objects to your controllers.
- Use joints to accurately pick up objects.
- Snap objects into place.
- Use haptic feedback.
It’s time for some VR action!
First, download the materials for this chapter using the Download Materials button at the top or bottom of this page.
Before diving straight into the deep end, take a moment to get familiar with the project.
In Unity, open the starter project for this tutorial and look at the Project window. As you can see, this project comes pre-loaded with many assets:
Here’s an overview of what’s inside:
- OculusPlatform, OVR & OvrAvatar: The APIs for the Oculus Rift.
- Plugins: Code libraries for the Oculus Rift.
- Resources: Contains the settings file for Oculus Avatar.
- Standard Assets: Unity’s standard character controllers and physics materials.
- SteamVR: The SteamVR plugin, along with its scripts and prefabs.
- VRTK: The Virtual Reality Toolkit plugin.
To get things started, open the Chapter 8 – Advanced Interactions scene from the Razeware\Scenes folder, and review the scene in the editor.
The scene comes with a few things already set up:
Here’s the breakdown:
- AdvancedFullChapter8: Contains all of the basic static meshes for the scene.
- BasicLight & ExtraLights: Various lights for the scene. ExtraLights is disabled by default.
- RW Props: Contains the statues in the scene and is the parent GameObject for the interactables.
- Chapter8 BGM: A simple GameObject with an Audio Source attached that plays classical music in the background. If that’s not your style, feel free to replace the music with some funky beats!
Now that you know this project inside and out, it’s time to get cracking.
Creating the VRTK Setup
By now, you may know how to prepare everything for VRTK, but to make it easier on yourself, you’re going to create a template setup which you can reuse.
Create a new GameObject, name it [VRTK_SDK_MANAGER], and reset its Transform component. This will serve as the parent for the different SDK setups.
Add a VRTK_SDK Manager component to the newly created GameObject, and check any of the SDK_OCULUS_AVATAR checkboxes; the other one will get checked automatically.
Add another empty GameObject as a child of [VRTK_SDK_MANAGER], name it [VRTK_SDK_SETUPS], and reset its Transform.
Now create the following GameObjects, reset their Transform, and parent them to [VRTK_SDK_SETUPS]:
When you’re done, the hierarchy in the SDK Manager will look like this:
To create parented instances, drag the [CameraRig] and [SteamVR] prefabs from the SteamVR\Prefabs folder onto the SteamVR GameObject.
Drag the OVRCameraRig prefab from the OVR\Prefabs folder onto Oculus to parent the prefab. Do the same for the LocalAvatar prefab from the OvrAvatar\Content\Prefabs folder.
Select OVRCameraRig and change the value of Tracking Origin Type to Floor Level. This helps position the camera correctly.
Now drag the VRSimulatorCameraRig prefab from the VRTK\Prefabs folder onto the Simulator GameObject.
Select the three SDK GameObjects you’ve just added, and add a VRTK_SDK Setup component to all of them. Select them all separately and assign the appropriate value to Quick Select for each one:
- SteamVR: SteamVR
- Oculus: Oculus (Rift)
- Simulator: Simulator
Now that you’ve added all of the SDK setups, let the SDK Manager auto populate its setup list by selecting [VRTK_SDK_MANAGER] and clicking on the Auto Populate button at the bottom.
That’s it for the SDK Manager. You’re ready to move on to the play area and controller aliases.
Play Area and Controller Aliases
As a child of [VRTK_SDK_MANAGER], create an empty GameObject. Reset its Transform and name it [VRTK_SETUP]. Create the following empty GameObjects and add them as children of [VRTK_SETUP]:
- Play Area
- Left Controller
- Right Controller
When you’re done, the resulting hierarchy will look like this:
Add a VRTK_Adaptive Quality component to Play Area, set its Msaa Level to 2, and set Scale Render Viewport between 0.5 & 2.5.
This adjusts the render resolution depending on the performance; it can both upscale and downscale, so you’ll always use the optimal resolution.
Now add a VRTK_Height Adjust Teleport component to the Play Area. This serves as the base teleport method for all scenes unless specified otherwise. You can leave the variables at their defaults.
Add the following components to both Left Controller and Right Controller:
- VRTK_Controller Events
- VRTK_Interact Touch
- VRTK_Interact Grab
Leave all of these components at their default values.
Add a VRTK_Pointer and a VRTK_StraightPointerRenderer component to Left Controller. This will add a straight pointer to your left controller which you can use to teleport.
Finally, click the selection button next to VRTK_Pointer’s Pointer Renderer slot, and select Left Controller.
Select VRTK_SDK_MANAGER, and hook up the controller aliases by dragging Left Controller and Right Controller to their respective slots.
Now that the SDK Manager is ready, create a new folder under Razeware named Prefabs. To turn the manager into a prefab, drag [VRTK_SDK_MANAGER] to the newly created Razeware\Prefabs folder.
Test if everything works correctly by taking a look around the scene with your preferred headset. If you find any issues, backtrack through this section and compare the steps with what you have.
The work you’ve done will prove to be a considerable time saver later on, but now it’s time to move on.
In the next section, you’ll learn how to add tooltips to GameObjects.
Tooltips are like small 3D signs you can use to label objects.
For this tutorial, you’ll be adding a name label to the armored horse, named “Knight”, in the middle of the museum. You may remember him from Unity Games by Tutorials!
To make a tooltip, you need two GameObjects: a root GameObject that will act as the target and the actual ObjectTooltip that’s provided by VRTK.
Start off by creating a container for tooltips; this helps to keep the scene nice and tidy.
Create a new GameObject, name it Object Tooltips, and reset its Transform component.
Now create the target for the knight by adding a new, empty GameObject as a child of Object Tooltips. Name it KnightTip and change its Position to (X:0, Y:1.8, Z:0).
Drag an ObjectTooltip prefab from the VRTK \ Prefabs folder onto KnightTip to make it a child.
Change the Position of ObjectTooltip to (X:1.35, Y:1.3, Z:-0.5), and change the following properties of its VRTK_Object Tooltip component:
- Display Text: Knight
- Font Size: 200
- Container Size: (X:600, Y:250)
- Line Width: 0.01
- Font Color: Pure White (R:255, G:255, B:255)
- Container Color: Pure Black (R:0, G:0, B:0)
- Always Face Headset: Checked
Here’s what your settings should look like when done:
All of these, except the last one, change the visuals of the tooltip.
Always Face Headset causes the tooltip to look at the player’s headset at all times so they will never be looking at the text sideways or from the back. The reason why Draw Line From & Draw Line To isn’t being set is because you’re using a parent GameObject, and the tooltip knows to draw the line from that position to itself.
All of these changes alter the look from this tiny tooltip for ants:
To this hulking, beefy powerhouse of a tooltip:
Well at least it’s easy to see, right?
To test if it’s working correctly, play the scene and look at the knight from different angles; the tooltip should always be visible!
That’s it for object tooltips, be sure to experiment with different styles in your projects.
Next up, the controller, which can also have tooltips!