Made With Unity | Unity VR Part 2: Interactors

Made With Unity | Unity VR Part 2: Interactors

·

3 min read

Small series of articles/tutorials about VR on Unity and using the Oculus Quest headset.

Interactors

There are two Interactor components that Unity gives us with OpenXR, XR Ray Interactor and XR Direct Interactor

XR Ray Interactor

The XR Ray Interactor is the one you have by default when you add a XR Origin (Action-Based) on your scene, the red stick ray that moves with your controllers; this allows you to interact with an object that can be far away if the red beam touches them.

The rays will interact with whatever object has the component XR Grab Interactable and the appropriate Interaction Layer Mask.

Example In Using XR Ray Interactor And XR Grab Interactable

I have three objects on the scene: a Cube, a Sphere, and a Cylinder. The Cube has the component script XR Grab Interactable; adding the script will automatically add a Rigidbody component to the object. Leaving everything as default, notice that both controllers can interact with just the Cube but not with the other objects. The Cube has the Interaction Layer Mask has Default, one of the layers the controllers can interact with.

Let's do another example:

Add the remaining objects, Sphere and Cylinder, the XR Grab Interactable and select Interaction Layer Mask, press Add layer… On the following windows put on User Layer 1 to 3 the names of the objects.

Give them their named layer on each object and disable the other; the following image has the Cube object with just the Cube layer.

Back to the XR Origin game object on the LeftHand Controller, set the Interaction Layer Mask to Cube, Cylinder, and Sphere; the RightHand Controller will have just the Sphere in the Interaction Layer Mask.

LeftHand Controller can interact with all three objects.

RightHand Controller can only interact with the Sphere.

In this example, the LeftHand Controller can interact with all the objects because it shares the same layer; the RightHand Controller can only interact with the Sphere.

Suppose you like to grab an object in a specific spot. In that case, all you have to do is add an Empty Game Object that has a child of the desired object to pick, name it AttachTransform, place it where it needs to be, and put it on the field Attach Transform in the XR Grab Interactable script component.

XR Direct Interactor

The XR Direct Interactor behaves similarly to the XR Ray Interactor; if it shares the same layers as the object to interact, it will pick it up. The critical difference is that you must be near the thing to interact with it. To use the XR Direct Interactor, you will only need the XR Controller (Action-based) script and the XR Direct Interactor on the same game object.

I added a Sphere game object as a child on both LeftHand and RightHand Direct and called it MockHand. I gave it a material color and removed the colliders on the Sphere. The LeftHand and RightHand Direct will have a Sphere Collider, and Is Trigger Enabled. Added the necessary layers on the Interaction Layer Mask.

Next up will be learning movements, turns, and teleportation concepts.

🎮[PART 3]🎮