Making objects grabbable

Our carnival midway is progressing nicely and with a teleporting function in place, the scene is becoming a fulfilling VR experience. In this next step, we will build the functions that make it possible to pick up and swing the matter for the Wacky Moles game and throw softballs in the Milk Bottle Toss game. If you have experience with the OVRGrabber and OVRGrabble scripts, feel free to implement the function and move on to the next section.

Follow along if this is your first time using these scripts:

  1. Drill down to the TrackingSpace of the items in the OVRPlayerController prefab, as shown in Figure 7.18.
  2. Right-click on the LeftHandAnchor and choose 3D Object | Sphere. Rename the sphere LeftGrabber and set its scale to (0.2, 0.2, 0.2).
  3. Complete this for the RightHandAnchor as well, naming the object RightGrabber. Figure 7.18 shows the LeftGrabber and RightGrabber as being added to children of each anchor:
Figure 7.18: Creation of the LeftGrabber and RightGrabber used to facilitate grabbing objects
  1. Hit Play and put on the headset.

The two spheres now snap into position and follow the hand movements of the touch controllers. Now that the objects are in place, we can add additional OVR scripts to the object anchors. Once the scripts are operational, the Mesh Renderer for the grabbers will be deactivated:

  1. Drag the OVR | Scripts | Util | OVRGrabber script to the LeftHandAnchor.
  2. The LeftHandAnchor will now have the OVRGrabber script and a Rigidbody component.
  3. Select the Rigidbody component. Uncheck Use Gravity and check Is Kinematic. Without this step, our hands would drop to the ground plane instead of following our motion.
  4. In the OVR Grabber (Script) component, change the Grab Volume size to 1.
  5. Click the selection target for Grip Transform and select LeftGrabber. Remember that the LeftGrabber is where our hand is, so setting the object as the Grip Transform for the LeftHandAchor means that when we pick up an object, it will lock into the position of the LeftGrabber sphere.
  6. Click the selection target for Element 0 of the Grab Volume and select LeftGrabber again. This will use the LeftGrabber collider to determine if we can pick up an object.
  7. Next, choose L Touch for the Controller value.
  8. Select the LeftGrabber and change the Sphere Collider Is Trigger value to true by checking the checkbox.
  9. Repeat steps 1-8 for the RightHandAnchor and RightGrabber.

The touch controllers can now pick up objects, but we must also define which objects can be grabbed. Since we have not created any game props, we will use a simple cube to test the function:

  1. Add a cube to the scene, rename it Block and scale it down to (0.2, 0.2, 0.2).
  2. Add a Rigidbody component and drag the OVR | Scripts | Util | OVRGrabbable script to the Block GameObject.
  3. Play the scene and grab the Block. You should be able to pick up the object, transfer it from hand to hand, and throw the block.

The last step is to replace the spheres with the avatar hands. To do this we will deactivate the Mesh Renderer of the spheres:

  1. Select the LeftGrabber and RightGrabber and uncheck the Mesh Renderer component in the Inspector panel. This method works because we are only using the spherical collider for each grabber.
  2. Save the scene and hit the Play button.

If all goes well, you should now have two hands instead of spheres in the scene. The hands will behave just like the spheres except that now the controllers will react to button presses, hovers, and trigger pulls.

Now that we understand how to make objects grabbable, we can implement this approach for using the mallet and softballs in our Midway games.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.111.85