Creating realistic physical hands
Implementing haptic feedback
Highlighting Interactable game objects
Adding Realistic Physical Hands
In this section, you’ll learn to set up realistic hand collisions for your avatar hands . Currently, the way your hands are set up, they pass through game objects without colliding with them, which is not very realistic. Whenever your hand encounters an Interactable game object, it should be allowed either to grab it or, if your intention isn’t to grab the object, to collide with it, allowing you to push the object around. You’ll learn to set this up by using the VRTK’s Trackers Collider Follower prefab.
In the hierarchy, select the VRTK SETUP game object, and create a new child game object and rename this new child game object “VRTK COLLIDER FOLLOWER.” Then, expand the “Packages” folder in the Project tab , locate the Tilia Trackers Collider Follower Unity package, and expand it until you reach its “Prefabs” folder. Drag and drop the Trackers Collider Follower prefab onto the VRTK COLLIDER FOLLOWER game object in the hierarchy. Rename this Trackers Collider Follower game object “Trackers Collider Follower Right,” as this collider follower will track the Interactor on your right hand.
Now, playtest the Demo scene, and select the Scene tab in the Unity editor. Double-click the Collider game object in the hierarchy and you should be able to see your right hand in the Unity editor. Click the Edit Collider button in the Sphere component of the Collider game object and ensure that your Sphere Collider is encompassing your right hand well. Adjust the collider properties further if required.
You also need to ensure that as soon as your Right Controller’s Grab button is pressed, the Trackers Collider Follower Right game object will be deactivated. This will guarantee that there’s no conflict between grabbing an Interactable and interacting with it by pushing it around.
You’ll need to hook up your Right Controller’s Grip button so that as soon as it’s pressed, the Trackers Collider Follower Right game object will be deactivated. This will ensure that you can’t push your Interactable game objects around when you’re in Grab mode. Similarly, the moment you ungrab an Interactable game object, the Trackers Collider Follower Right game object will be activated. This ensures that you can push the Interactable game objects around again when you’re not in grabbing mode.
Let’s set this up now. Expand the Buttons Input Actions game object in the hierarchy, and select the Right-Hand Grab game object. In the Inspector, you’ll see that the Activated and Deactivated events for this Right-Hand Grab game object have an animator action setup.
Playtest the Demo scene using your VR headset. Approach the worktable and push the Hammer, Axe, and Drill Machine around with your open hand. You’ll find that these objects are easy to push around. Now, press the Grip button on your Right Controller so that your hand forms a clenched fist, and then approach any of the Interactable game objects on the worktable. Try pushing around any Interactable game objects using your right hand with your fist clenched and the Grip button on your Right Controller still pressed. You won’t be able to push these Interactable game objects around with your hand in this position. Last, try grabbing an Interactable game object, and note that you can grab it as before, as discussed in Chapter 8. Now, set up a Trackers Collider Follower Left game object for your Left Controller following these same steps. I will leave this as an exercise for you to complete on your own.
As the last step, you need to avoid any uncalled-for jittering behavior that may arise when the Trackers Collider Follower Right game object that is now following your right hand encounters your pseudo-body. In the hierarchy, select the Collider game object, nested within the Trackers Collider Follower Right game object, and set its Layer property to Transparent FX in the Inspector.
You can now be assured that the collider following your right hand won’t be able to interact with your pseudo-body but will be able to interact with Interactable game objects.
Set this up for your Trackers Collider Follower Left game object, too.
Obtaining Haptic Feedback
In this section, we’ll become familiar with how to obtain haptic feedback against our Left and Right Controllers whenever we grab an Interactable game object. Note that the haptic feedback provided against the Unity XR Rig is not great, so minimal feedback will be obtained when using the Unity XR Rig.
To get started, expand the “Packages” folder in the Project tab, and locate the Tilia Output Interactor Haptics Unity package and expand it until you reach its “Prefabs” folder. Now, drag and drop the Output Interactor Haptics prefab onto the VRTK SETUP game object in the hierarchy and select it. Ensure that the Interactor Haptics Facade component has been expanded in the Inspector.
Now, whenever you grab an Interactable game object using your right hand, your Right Controller will emit a short haptic pulse. A similar haptic pulse will be emitted by your Left Controller when you grab an Interactable game object with your left hand.
Highlighting Interactable Game Objects
The VRTK provides you with some very basic shaders that you can use to highlight your Interactable game objects upon touching them. It lets you provide the player with a visual cue indicating that an Interactable game object has been touched. To achieve this, we’ll use one of the six shaders provided by the VRTK. Of course, if you have access to a better-quality shader, you could use that one instead. To achieve this highlighting, the VRTK provides you with a Tilia package called Tilia Visuals Interactable Highlighter Unity . In this section, we’ll have the Hammer game object get highlighted when either of your hands encounters (touches) it.
To set this up, expand the “Packages” folder in the Project tab, and locate the Tilia Visuals Interactable Highlighter Unity package and expand it until you reach its “Prefabs” folder. In the hierarchy, select the VRTK SETUP game object and expand it. Then, drag and drop the Visuals Interactable Highlighter prefab onto the VRTK SETUP game object in the hierarchy and select it.
Now, test the Demo scene using your VR headset. Approach the Hammer and touch it with either your left or right hand, and you’ll see that the chosen Ambisonic Object material immediately gets highlighted on the Hammer. When you actually grab the Hammer, the highlight disappears. Using the Visuals Interactable Highlighter prefab makes it extremely simple to highlight your Interactable game objects. You now need to set up the Axe game object to be highlighted upon being touched. I’ll leave this as an exercise for you to complete.
Summary
In this chapter, you learned a few tips and tricks that can be used within any of your VR games or apps. We started by going over how to set up realistic physical hand collisions against our avatar hands. We achieved this by using the VRTK’s Trackers Collider Follower prefab. We saw how to get the Sphere Collider to encompass our right hand well. We then set up the Activated and Deactivated events to activate and deactivate the Trackers Collider Follower. Finally, we set up the Layer Collision Matrix so that the game objects assigned to the Transparent FX and Ignore Raycast layers wouldn’t collide with each other. In the next section of the chapter, we became acquainted with how to obtain haptic feedback against our controllers upon grabbing an Interactable game object. This was made possible using the VRTK’s Output Interactor Haptics prefab. We learned to set up several properties against this prefab. We also learned to set up the Grabbed event on the Interactions Interactor Left and Right game objects, allowing your controllers (Interactors) to receive haptic feedback. Last, we found out how to have an Interactable game object get highlighted upon touching it using the VRTK’s Visuals Interactable Highlighter prefab. Here, we selected the Interactable object we wanted to highlight and provided it with a suitable highlighting material. We ended the chapter by testing this highlighting mechanic.