The Unity DistanceGrab sample scene demonstrates how you can implement custom hands that can point at distant objects and have them zoom into their hands when the grip trigger is pulled. If an object is close enough to the hand to be grabbed normally, without zooming, it is grabbed in the same way as an Oculus Avatar’s hands. The user can also move around using the thumbsticks to demonstrate targeting range.
See the CustomHands and CustomControllers sample scenes for more demonstrations of hand usage in VR.
The goal of this topic is to introduce you to the prefabs, Game Objects, components, and properties that make this functionality work. You can also use this sample scene as a starting point for your own application.
Here are brief descriptions of the key prefabs and other Game Objects that make the core distance grabbing functionality in this scene work. These are all described in further detail later in this topic.
DetectGrabRange
Game Object added to the prefab that is not normally there.OVRPlayerController
that notifies grabbable objects when they are within grabbing range, at which point they become outlined in a color (or show a target) to indicate they are grabbable. The grabbing range itself is defined by a Sphere Collider
component (as trigger) on this object. Developers can use this Game Object as a guide when creating an object with similar functionality.The OVRPlayerController
prefab includes components and child objects necessary for 3D control in a VR environment. It also contains a child OVRCameraRig
to serve as the user’s VR camera and provide access to OVRManager
.
Select OVRPlayerController
in the Hierarchy and look at it in the Inspector window. Notable components include the following:
This standard Unity component is a capsule-shaped collider that can be directed to move around while being affected by collisions. Combined with the OVRPlayerController (Script)
component, it provides for first-person 3D movement with a decent variety of options.
Implements a basic first-person controller for VR. The controller will interact properly with a Unity scene, provided the scene has collision detection assigned to it. OVRPlayerController (Script)
contains a few variables attached to sliders that change the physics properties of the controller. For more information on OVRPlayerController
, see Oculus Utilities for Unity.
Allows you to experiment with certain sample scene settings, such as Speed Rotation Increment, which controls how quickly speed and rotation change based on input.
Component that shows debug information in a HUD when the Space Bar is pressed while the scene is running.
This child object of the OVRPlayerController
notifies grabbable objects when they are in grabbing range. As the user moves around the scene, grabbable objects will move into and out of grabbing range.
Select OVRPlayerController
in the Hierarchy and look at it in the Inspector window. Notable components include the following:
Notifies objects with a DistanceGrabbable
component within the Sphere Collider
’s Radius
that they are in grabbing range.
The Radius
property component is used to establish the grabbing range around the OVRPlayerController
.
As the user moves around, colored outlines (or targets) appear and disappear on grabbable objects as they pass into and out of range. When an object is within range, one of two colors will outline the object. One color (in this sample, white) indicates that an object is in grabbing range. Another color (blue), indicates that an object is being actively targeted by the user and can be grabbed by pressing the grip trigger.
These prefabs contain the custom hands as well as the components and child objects necessary to grab distant and nearby objects.
Select either of these prefabs in the Hierarchy and look in the Inspector window. Notable components include the following:
A standard Unity component that allows physics to be applied to an object. It is required to allow physical interaction with other objects in the scene.
Use Gravity is disabled to prevent gravity from affecting the hands. Is Kinematic is enabled to prevent hand movement from being affected by events such as collisions while still preserving their ability to physically interact with certain objects.
This component enables usage of a custom hand. The prefabs have default custom hands attached.
This component enables the hands to grab distant and nearby objects that have been configured with a DistanceGrabbable
component. This component is key to the main functionality of this sample scene, and its properties are as follows:
DetectGrabRange
Sphere Collider
’s radius.OVRPlayerController
that the hand is attached to.This section looks at one of the many grabbable objects in the scene in detail, but the information applies to all of them, regardless of shape or size.
In the Hierarchy, go to Environment > Dynamic > WoodBlocks > DistanceGrabWoodBlockPf and look at the Game Object in the Inspector window.
In addition to mesh-related components present on all models, grabbable objects need to be configured with the following components to work properly:
A standard Unity component used to define the shape of an object for physical collision and interaction. This is what DistanceGrabber
is looking at when it tries to grab an object. Some objects in the scene use Sphere Colliders when the shape of the object is appropriate.
A standard Unity component that allows physics to be applied to an object. It is required to allow physical interaction with other objects in the scene.
Although RigidBody
is also used when making grabbers, its settings differ here. For grabbable objects, Use Gravity is enabled to allow for realistic throws and collisions with other interactive objects. Is Kinematic is disabled to allow physics to have full effect over the object.
This key component enables the object to be grabbed by Avatar hands that have been configured with an DistanceGrabber
component. This component is key to the main functionality of this sample scene, and its properties are as follows:
DistanceGrabber
that the grabbed object’s position and/or orientation can snap to.The DistanceGrabHandLeft
and DistanceGrabHandRight
prefabs can easily be dropped into your own app to help get you started in implementing distance grabbing functionality in your own app. These prefabs include custom hands, but you can remove the Hand component from each prefab in order to make them suitable for use with Oculus Avatars.
Grabbable objects must be configured with DistanceGrabbable
, RigidBody
, and an appropriate collider in addition to standard mesh-related components.
There is no prefab for DetectGrabRange
. You can make your own by adding a Game Object to the OVRPlayerController
and adding the Oculus Grab Manager
component to it along with a Sphere Collider
.
An important implementation consideration for distance grabbing is the range at which objects can be grabbed. To change the grabbing range of your hands, you must change three values:
Radius
property on DetectGrabRange
’s Sphere Collider
component.Max Grab Range
properties on DistanceGrabHandLeft
’s and DistanceGrabHandRight
’s DistanceGrabber
component.The Radius
property of DetectGrabRange
’s Sphere Collider
creates an area around the OVRPlayerController
that informs objects with a DistanceGrabbable
component that they are in grabbing range and available as a target to objects with a DistanceGrabber
component. In short, DetectGrabRange
determines the range of what can be grabbed.
However, it’s DistanceGrabHandLeft
’s’ and DistanceGrabHandRight
’s’ DistanceGrabber
components that determine what is being actively targeted. The DistanceGrabber
s’ Max Grab Distance
s must at least match the Radius
of the Sphere Collider
. It’s recommended that the Max Grab Distance
be slightly larger than the radius to compensate for unpredictable real-world hand/body positioning.