Create Ray Interactions
In this tutorial, you learn how to use ray interactions to project a ray from your hands, controllers, or controller driven hands onto a cube. Ray interactions are an easy way to interact with user interfaces or distant objects. To try ray interactions in a pre-built scene, see the
RayExamples scene.
Ray interactors let you initiate a raycast with your hands, controllers, or controller driven hands. There are two types of ray interactor, HandRayInteractor for hands and controller driven hands, and ControllerRayInteractor for controllers.
Adding Hand Ray Interactors for Hands - Open the Unity scene where you set up your hands.
Under Project, search for HandRayInteractor, and drag the HandRayInteractor prefab from the search results into the Hierarchy onto OVRInteraction > OVRHands > LeftHand > HandInteractorsLeft.
Your hierarchy should look like this.
- Under Hierarchy, select HandInteractorsLeft.
- Under Inspector, in the Best Hover Interactor Group component, click the + in the Interactors list to add a new element.
Set the element to HandRayInteractor by dragging HandRayInteractor from Hierarchy to the element’s property.
- Repeat these steps for the right hand.
Select File > Build And Run, or if you have a Link connected, click Play.
The current scene loads. A small icon appears between your pointer finger and thumb to indicate where the ray is pointing. When you touch your pointer finger and thumb together, the icon changes to a white line to indicate you’re selecting an object (if one exists).
Adding Hand Ray Interactors for controller driven hands - Open the Unity scene where you set up your controller driven hands.
Under Project, search for HandRayInteractor, and drag the HandRayInteractor prefab from the search results into the Hierarchy onto OVRInteraction > OVRControllerDrivenHands > LeftControllerHand > ControllerHandInteractors.
Your hierarchy should look like this.
- Under Hierarchy, select ControllerHandInteractors.
- Under Inspector, in the Best Hover Interactor Group component, click the + in the Interactors list to add a new element.
Set the element to HandRayInteractor by dragging HandRayInteractor from Hierarchy to the element’s property.
- Repeat these steps for the right controller hand.
Adding Controller Ray Interactors - Open the Unity scene where you set up your controllers.
Under Project, search for ControllerRayInteractor, and drag the ControllerRayInteractor prefab from the search results into the Hierarchy onto OVRInteraction > OVRControllers > LeftController > ControllerInteractors.
Your hierarchy should look like this.
- Select ControllerInteractors.
- Under Inspector, in the Best Hover Interactor Group component, click the + in the Interactors list to add a new element.
Set the element to ControllerRayInteractor.
- Repeat these steps for the right controller.
Select File > Build And Run, or if you have a Link connected, click Play.
The current scene loads. A white beam emitting from each controller indicates where each ray is pointing. When you pull the controller’s trigger, the beam changes from white to blue to indicate you’re selecting an object (if one exists).
A ray interactable lets you interact with the object it’s attached to.
- Add a Cube GameObject to your scene by right-clicking in the Hierarchy and selecting Create Object > Cube.
- Name it Cube.
- Position Cube in front of the camera.
- Add a child GameObject to Cube named Collider by right-clicking Cube and then selecting Create Empty.
- Select Collider.
- Under Inspector, add a Box Collider and Collider Surface component.
- In the Collider Surface component, set the Collider property to the Collider GameObject.
- Under Hierarchy, select Cube.
- Under Inspector, add a RayInteractable component. This component will detect the ray.
Under Inspector, in the RayInteractable component, set the Surface property to Collider.
Select File > Build And Run, or if you have a Link connected, click Play.
When the current scene loads, use your hands or controllers to hover over the cube. A circular cursor appears on the cube to show where the ray is pointing. When you touch your pointer finger and thumb together or pull the trigger, the cursor changes to a solid white color.