In Chapter 6, I mentioned that the VRTK provides you with two standard ways to interact with an Interactable game object. Direct Interaction is the most common form of Interaction. The other form of Interaction is the ability to grab an object from a distance.
The Distance Grab mechanic allows you to grab an object from a distance without physically approaching the object. It provides you with an invisible beam with a visible reticle to identify Interactable objects in the VR world and allows such Interactable objects to be grabbed from a distance by simply pressing the Grab button on the Interactor.
In VR, you have the projective ability to influence a physical object without physically interacting with it. If you as the player drop your weapon in a VR FPS (First Person Shooter) game, you ideally don’t want to physically bend down to pick it up. You can avoid doing this by turning on the Distance Grab mechanic, which will allow the weapon move into your hand.
In this chapter, we’ll revisit the grabbing mechanic by setting up a Distance Grabber. The VRTK provides you a distance grab pointer in the form of a blue pulsating reticle that shows up when you point toward an Interactable game object. This is the standard out-of-the-box approach provided to you by the VRTK.
If you’re some distance away from the Interactable game object you want to grab, and if the object is not large enough, it becomes difficult to point precisely at that game object and have the blue pulsating reticle show up. To overcome this drawback, you’ll learn to set up a straight pointer that aids in precisely pointing at the Interactable game object and performing the distance grab a lot more easily.
You’ll also learn to limit the grab distance so that you can’t grab an object from, say, 100 meters away, though if your game requires that, you could set that up as well. Along the way, you’ll learn how to set up a Toggle Action.
Prerequisites for Distance Grabbing
Before setting up the Distance Grabber, we need to ensure that on the Oculus-provided Custom Hand Right, we deactivate the Capsule Colliders in its Grab Volume Big and Grab Volume Small child game objects. This is essential for having the distance grab pointer and its reticle emit from your hand.
Setting Up the Distance Grabber
Select and expand the VRTK SETUP game object in the hierarchy and create a new empty child game object and rename it “VRTK DISTANCE GRAB.” Select the Project tab and expand the “Packages” folder. Locate the Tilia Interactions Pointer Interactors Unity package, and expand it until you reach its “Prefabs” folder.
With the “Prefabs” folder selected, drag and drop the Interactions Pointer Interactors Distance Grabber prefab in the right pane onto the hierarchy’s VRTK DISTANCE GRAB game object. Rename this prefab “Interactions Pointer Interactors Distance Grabber Right,” as it’s set up against your right hand.
Ensure that the Distance Grabber Facade component in the Inspector has been expanded. In the “Interaction Settings” section, the Interactor property determines which Interactor the Distance Grabber is associated with. In this case, you will associate it with the right-hand Interactor. However, you can also associate it with the left-hand Interactor if you’re left handed. The procedure is the same. If you would like both of your hands to perform a distance grab, you need to add another Distance Grabber prefab for your left hand and associate it with your left-hand Interactor.
To set up the Distance Grabber against your right-hand Interactor, expand the Camera Rigs, Tracked Alias game object in the hierarchy, then expand its Aliases game object, and then expand its Right Controller Alias game object. Drag and drop the Interactions Interactor Right game object from the hierarchy into the Interactor property parameter in the Distance Grabber Facade component.
Test the Demo scene using either the Spatial Simulator or your VR headset. Point your right hand at the Drill machine on the table. You should notice a light-bluish pulsating reticle appear over the Drill machine. This cue serves as an indicator that you may now distance grab the Drill machine. When the reticle appears over the Drill machine, the moment you press the right mouse button or the Grab button on your right controller, the Drill machine will instantly snap to your hand.
Now, move further away from the table and try pointing your hand at the Drill machine to get the bluish pulsating reticle to appear. You’ll find it difficult to precisely point your hand at the Drill machine. To solve this problem, you will create a straight ray pointer that will emit from your hand, allowing you to easily point at Interactable objects at a distance and grab them.
If you don’t want the Drill machine to snap to your hand instantly, you can configure a telekinesis movement, where y the Drill machine will slowly levitate toward your hand. We’ll set up both these functionalities next.
Setting Up a Telekinesis Grab
Note that you weren’t required to populate the Follow Source property in the Interaction Settings section, as doing this is optional. If you don’t populate this Follow Source property, the current Interactor is used by default.
Now, let’s set up the telekinesis movement and have a grabbed Interactable object levitate toward your hand over half a second. In the Distance Grabber Facade component, set the Transition Duration property value to 0.5. With this setting, the Drill machine or any other selected Interactable game object will take half a second to reach your hand from the moment you press the Grab button. The higher the Transition Duration property value, the longer the selected Interactable game object will take to reach your hand.
Playtest the Demo scene now and try out your new levitating behavior. You’ll see that the Drill machine no longer instantly snaps to your hand. It instead moves slowly toward your hand over half a second.
Setting Up a straight pointer to Grab Interactable Objects
Next, let’s set up a straight pointer to show up, which will be easier to point at an Interactable object. Select the Interactions Pointer Interactors Distance Grabber Right game object in the hierarchy, and expand it until you reach its Pointer Elements game object.
You’ll need to navigate as follows: Interactions Pointer Interactors Distance Grabber Right ➤ Internal ➤ Pointer ➤ Indicators Object Pointers Straight ➤ Pointer Elements, see Figure 15-2.
Now expand this Pointer Elements game object in the hierarchy and you’ll see that it contains three child game objects: Origin, Repeated Segment, and Destination. Select all three. In the Inspector, ensure that the Pointer Element component has been expanded.
Playtest your Demo scene using the Spatial Simulator or your VR headset. You’ll notice a red straight pointer emanating from your right hand. Aim the red pointer at the Drill machine, and you’ll see it turns green and the bluish pulsating reticle is visible at the end of this green-ray pointer. Press the right mouse button or the Grab button on your right controller, and your Drill machine will levitate toward your hand.
Now, place the Drill machine back on the table. Move back so that you’re positioned near the Container behind the table. You’re now quite a distance away from the Drill machine. Point your Distance Grabber’s straight pointer toward the Drill machine so that it turns green. Press the right mouse button or the Grab button on your right controller and grab the Drill machine again. You’ll find that it’s now a lot easier to grab the Drill machine with the assistance of the straight pointer.
Changing the Straight Pointer’s Grabbing Distance
Let’s now learn how to change the grab distance for the straight pointer. We’ll reduce this distance so that we can only grab Interactable game objects that are within a ten-meter radius.
Playtest your Demo scene again using the Spatial Simulator or your VR headset. You’ll see that if you’re within a ten-meter distance from the Drill machine, you’ll be able to grab it, but if you move beyond this distance, you won’t any longer be able to do so.
You may have noticed that currently your Distance Grabber’s straight pointer is always on regardless of whether you intend to do a distance grab. In the next section, we’ll learn how to activate and deactivate the straight pointer as we deem fit.
Activating and Deactivating the Distance Grabber
In the previous section, our Distance Grabber was set to always be on, with the straight pointer displayed. In this section, we’ll learn to set up the A button on the Oculus controller, the X button on the Xbox controller, and the Spacebar key on the keyboard to enable the Distance Grabber to be toggled on and off. Since the HTC Vive doesn’t provide the A, B, X, or Y buttons as does the Oculus controller, you’ll use the Right Trackpad Touch 17 button action to toggle the Distance Grabber on and off.
To achieve this, we’ll utilize a new Toggle Action component provided by the VRTK that allows you to have your buttons and key presses function like a toggle. When you press the Spacebar key or the appropriate button on your controllers once, you’ll activate the Distance Grabber along with its pointer. Pressing either of them again will deactivate these items.
Let’s first set up a simple Toggle game object with a Toggle Action component on it. Select the Button Input Actions game object in the hierarchy and create a new empty child game object and rename it “Toggle.” Then, click the Add component button in the Inspector and add a Toggle Action component to this game object.
You now need to set up the Activated and Deactivated events against the Toggle Action component of the Toggle game object. To do so, expand the Activated and Deactivated events. Click the plus symbol located in the bottom right corner of each of these events to add an event listener to each of them. Then, drag and drop the Interactions Pointer Interactors Distance Grabber Right game object from the hierarchy into the event listener box of the Activated event. For the function, select Game Object, Set Active in the “Static Parameters” section, and ensure you check the box below, which is equivalent to setting a True value, see Figure 15-4.
Essentially , what you’re doing is activating the Interactions Pointer Interactors Distance Grabber Right game object when the Activated event is fired. This occurs when you press either the appropriate controller button or the spacebar key the first time, which results in the Toggle being activated.
Next, drag and drop the Interactions Pointer Interactors Distance Grabber Right game object from the hierarchy into the event listener box of the Deactivated event. For the function, select Game Object, Set Active in the “Static Parameters” section and ensure you uncheck the box below, which is equivalent to setting a False value, see Figure 15-4.
Now it’s time to capture the various inputs discussed at the beginning of this section, which, when triggered, will result in toggling your Distance Grabber on and off. Let’s begin by capturing the Spacebar key press against the keyboard.
In the hierarchy in the VRTK SETUP game object, locate and expand the VRTK INPUT CONTROLLERS game object. Then, expand its Keyboard Input game object. Duplicate the Input Unity Input Manager Button Action T game object and rename the copied game object “Input Unity Input Manager Button Action Spacebar.” This will be used to capture a spacebar key press. In the hierarchy, with the Input Unity Input Manager Button Action Spacebar game object selected, locate the Key Code property in the Unity Input Manager Button Action component in the Inspector. From its drop-down, select the Space key menu item. You can now be assured that a spacebar key press will be captured.
Let me now explain how this works. The moment you press the Spacebar key on the keyboard, the Unity Input Manager, Button Action Spacebar game object fires its Activated event. This event calls on your Toggle game object that has been fitted with a Toggle Action component, asking it to receive a Boolean value. This sets the Toggle Action to On—that is, a True state—causing the Activated event in the Toggle Action component to trigger, which results in your Distance Grabber game object being activated.
Note your Toggle Action is now set to On—that is, a True state. The next Spacebar key press received by the Toggle Action will toggle it off. When you press the Spacebar key another time, your Input, Unity Input Manager, Button Action Spacebar game object will fire its Activated event again. This event calls on your Toggle game object that has been fitted with a Toggle Action component, asking it to receive a Boolean value, which will set this Toggle Action to Off—that is, a False state—causing the Deactivated event in the Toggle Action to trigger, which results in your Distance Grabber game object being deactivated.
You are thus toggling the Distance Grabber and its straight pointer on and off via the Toggle Action component on your Toggle game object. At this point, you can run the Demo scene in Spatial Simulator mode and hit the Spacebar key on the keyboard to see the straight pointer of your Distance Grabber toggle off and on. In the hierarchy, you’ll also notice that the Interactions Pointer Interactors Distance Grabber Right game object is enabled and disabled upon pressing the Spacebar key.
As discussed at the beginning of this section, let’s set up the A button on the Oculus controller, the X button on the Xbox controller, and the Right Trackpad Touch 17 button on the HTC Vive to enable the Distance Grabber to toggle on and off. In the hierarchy, with the VRTK INPUT CONTROLLERS game object expanded, also expand the Oculus Touch Right controller game object and Input Actions and select the Button One Press [0] game object. In the Inspector, ensure that the Unity Input Manager Button Action component has also been expanded. Then, locate and expand the Activated event. Click the plus symbol located in the bottom right corner to add an event listener. Drag and drop the Toggle game object from the hierarchy into the event listener box of this Activated event. For the function, select Toggle Action Receive from within the “Dynamic Bool” section.
With the VRTK INPUT CONTROLLERS game object expanded, collapse the Oculus Touch Right Controller game object. Expand the X-Box Controller game object, then expand Input Actions, and select the Button X Press [2] game object. In the Inspector, ensure that the Unity Input Manager Button Action component has been expanded. Locate and expand the Activated event. Click the plus symbol located in the bottom right corner to add an event listener. Drag and drop the Toggle game object from the hierarchy into the event listener box of this Activated event. For the function, select Toggle Action Receive from within the “Dynamic Bool” section.
Finally, let’s set up the Right Trackpad Touch 17 button on the HTC Vive to toggle the Distance Grabber on and off. In the hierarchy, with the VRTK INPUT CONTROLLERS game object expanded, collapse the X-Box Controller game object. Expand the Open VR Right Controller game object, then expand Input Actions and the Right Trackpad game object, and select the Right Trackpad Touch 17 game object. In the Inspector, ensure that the Unity Input Manager Button Action component has been expanded. Locate and expand the Activated event. Click the plus symbol located in the bottom right corner to add an event listener. Drag and drop the Toggle game object from the hierarchy into the event listener box of this Activated event. For the function, select Toggle Action Receive from within the “Dynamic Bool” section.
Before proceeding to test your Demo scene, ensure that you deactivate the Interactions Pointer Interactors, Distance Grabber Right game object, as outlined in Figure 15-8. You don’t want your Demo scene to start with the Distance Grabber showing. Select the Interactions Pointer Interactors, Distance Grabber Right game object in the hierarchy and uncheck its box in the Inspector to deactivate it.
Now, playtest your Demo scene using the Camera Rigs, Spatial Simulator via the keyboard and the Xbox controller. Also, ensure that you test using your VR headset using either the Camera Rigs, Unity XR or Camera Rigs, Oculus Integration setup.
Once your Demo scene loads, you’ll see that your Distance Grabber’s straight pointer isn’t initially visible. Press the A button on your Oculus right controller or the X button on your Xbox controller, tap the Right Controllers trackpad of your HTC Vive, or press the Spacebar key on the keyboard to activate the straight pointer on the Distance Grabber.
Now perform a distance grab against the Drill machine. Also, try turning off your Distance Grabber’s straight pointer by pressing the button you used to toggle it on a subsequent time. Note that pressing the defined toggle button once toggles the straight pointer on and pressing it again toggles it off. Play around with your newly created Toggle and distance grab mechanic.
Automatically Deactivating the Distance Grabber
Last, let’s include a final subtle game mechanic wherein the Distance Grabber’s straight pointer is automatically turned off upon grabbing the Drill machine, which eliminates the need to turn it off manually. This makes sense as you’ve already grabbed onto an Interactable game object with your right hand and there is nothing more you can grab, so you may as well have the straight pointer turn off automatically. Let’s set this up.
In the hierarchy, select the Toggle game object. Click the plus symbol located in the bottom right corner of the Activated event to add a new event listener. Now, select the Interactions Pointer Interactors, Distance Grabber Right game object and expand it until you locate its nested Pointer game object, as shown in Figure 15-9.
This ensures that the Pointer game object is explicitly activated, which for some bizarre reason, doesn’t happen. In the case where once a grab has occurred, the straight pointer on the Distance Grabber is automatically turned off, then subsequently attempting to turn it on fails. Even though the Interactions Pointer Interactors, Distance Grabber Right game object gets activated, its child Pointer game object remains inactive for some inexplicable reason. Here you’re forcibly activating this nested child Pointer game object.
Now, playtest your Demo scene using the Camera Rigs, Spatial Simulator via the keyboard and the Xbox controller. Ensure that you test using your VR headset using either the Camera Rigs, Unity XR or Camera Rigs, Oculus Integration setup. Note that you can toggle the Distance Grabber’s straight pointer on and off as expected. Now, with your the straight pointer turned on, distance grab the Drill machine. You’ll see that the moment the Drill machine has been grabbed, the straight pointer automatically gets deactivated.
Summary
In this chapter, we found out about a new way of interacting with Interactable objects, the distance grab mechanic. You learned about and created one, offering you the projective ability to manipulate a physical object without physically interacting with it. We started by reviewing how distance grabbing works in the VRTK. We then went over the specific prerequisites for getting distance grabbing to work. We then learned how to set up our Distance Grabber, as well as a telekinesis grab. We saw that with just the blue reticle present, it becomes difficult to distance grab, so we set up a straight pointer that is immensely useful in grabbing Interactable objects from a distance. You learned how to adjust this straight pointer’s grab distance. You then learned how to activate and deactivate the straight pointer using a button press. Finally, you learned how to automatically deactivate the straight pointer when a Interactable object grabbed from a distance reaches your hand.