This blog covers adding Gear VR Controller support to Unity projects. The provided GearVRPointer script mimics how the Gear VR Controller works in Gear VR Home, it creates a visual line to help find where the user is pointing.
Download and import Oculus Utilities For Unity 5 into the Unity project. Add a OVRCameraRig prefab to the scene, this will be the camera used for VR. The prefab can be found at
Assets/OVR/Prefabs/OVRCameraRig.prefab. If any scripts rely on a Camera component or the transform of an actual camera, use the CenterEyeAnchor object within the OVRCameraRig prefab. The CenterEyeAnchor appears in the hierarchy under:
Do not try to move the camera directly. If the camera needs to move in 3D space, move the transform of the top level OVRCameraRig object. For more information on how OVRCameraRig works, check the links in the Further Reading section.
Add two instances of the GearVrController prefab to the scene, one under
OVRCameraRig/TrackingSpace/LeftHandAnchor and one under
OVRCameraRig/TrackingSpace/RightHandAnchor. The local transform of these prefab instances should be set to identity. The GearVrController has a OVRGearVRController component attached. This component hides or shows the controller prefab if the controller is available. Select LTrackedRemote and RTrackedRemote as the controller types for the left and right hand controllers in the OVRGearVRController instances.
Add Controller Support
Add a new LineRenderer component to the OVRCameraRig object. This component will be responsible for rendering the selection ray. To mimic the white ray seen in Oculus Home, configure the LineRenderer with the following settings:
All of the settings which need to be changed are highlighted in the below image:
Download the attached GearVRPointer script, and attach it to the OVRCameraRig root object. Configure the OVR Rig Transforms that the script exposes to the appropriate transforms under OVRCameraRig. Set the OVRCameraRig its self as the line renderer object.
The project should now have Gear VR Controller support, similar to how the controller functions in Gear VR Home. Build the project and run it on a Gear VR headset to confirm.
The GearVRPointer script provides callbacks for when the user hovers over objects and for when the user selects objects. These callbacks do not work with Unity's UI System, see the Further Reading section for more info on UI. All callbacks have the same signature, the callback function is expected to return nothing and take a single argument, the Transform of the object being affected.
There are three callbacks for selection: one for when the user selects something using the trigger button, one for when the user selects something with the touch pad and the last one fires when the user uses either the trigger or the touch pad to select something. In most simple cases, use the On Select callback as it is fired with both the trigger and touch pad.
Selection works similar to how it does with a mouse, the user has to press down and release on the same object. For example if the user presses down on one object, keeps the trigger pressed and moves the pointer outside the object, releasing the trigger outside the object will not result in a selection. Similarly, pressing the trigger outside of an object then hovering over an object and releasing the trigger will not cause a selection.
Want to make the pointer work with Unity's UI system? This article covers how to do so: Adding Gear VR Controller Support to Unity's UI
Interested in why OVRCameraRig is set up the way it is, and how to support the Gear VR Controller with a different camera setup? This article covers how to: Adding Gear VR Controller Support to the Unity VR Samples