Unity VR : Direct Interactor

Unity VR : XR Direct Interactor

VR allows us to interact with the gaming world in a way that we have never been able to before. We now have the ability to reach out and grab thing within the world as if we were there. The question is, how do we do that with Unity and the XR Interaction toolkit?

That question will be answered here! In this tutorial, I’ll go over in detail the XR Direct Interactor and how we can use it.

To follow along, please download the Github project here and go to the Scene titled XR Direct Interactor.

1 : Adding XR Direct Interactor to our Hands

Loading into the Scene and navigating to the LeftHand and RightHand Controllers, you can see that it has the XR Controller and Hand Script. In order to make it able to grab things, we need to add two things. Click add component on the LeftHand or RightHand Controller and add the following.

  • XR Direct Interactor
  • Sphere Collider
The Sphere Collider is used by the XR Direct Interactor to determine whether an object is in range to be grabbed. Under the Sphere Collider, select “Is Trigger” and reduce the radius to 0.1. This will make the Collider about the size of our hand. 

If you start up the Scene now, you should be able to grab the ball directly in front of you. 

2 : XR Direct Interactor Attributes

Lets go over the XR Direct Interactor Attributes and what they do.

  • Interaction Manager – This manager is in charge of all interactions between the interactable object and the XR Direct Interactor. If there is one in the scene, the XR Direct Interactor will automatically assign itself to it. You can have multiple Interaction Managers if you wanted to customize certain interactions, but I haven’t found a good use case for this yet.
  • Interaction Layer Mask –  If you want to have only certain objects be interactable, you would use this layer mask to set that up. The default setting is to allow all interactable objects
  • Attach Transform –  This is the Transform that an interactable object will attach to. If you don’t set one, it’ll use the XR Direct Interactor’s transform by default.
  • Select Action Trigger To determine how the Select Action is triggered, you use this. The Select Action can be done in four ways. By default it is by current state. It can also be based off state change, toggle (when the select button is pressed), and stick toggle (it is activated when the select button is pressed and deselected when the select button is depressed a second time).
  • Hide Controller on Select –  Toggling this will hide the hand prefab when you select an object. This is great for getting around having to make proper attach points for every object in your Scene.
  • Allow Hovered Activate –  This sends out a activate and deactivate event to the interactable object when hovered over.
  • Sound, Haptic, and Interactor Events –  These events can be used to provide feedback for the player. Interactor Events are things like Hover Enter/Exit and Select Enter/Exit. The Audio and Haptic events will allow you to determine how much vibration to send to a controller or play a sound when something is selected.

3 : Interaction Layer

A common solution that is needed is having interactors that don’t interact with every object they come in contact with. Here I’ll provide a quick guide on how to use the Interaction Layer to prevent your XR Direct Interactor from interacting with all objects.  

Start by selecting the LeftHand Controller. Drop down the menu for Interaction Layer Mask and click Add Layer. For User Layer 1, add a Layer called Interactable. Select the LeftHand Controller again and change the Interaction Layer Mask to Interactable. 

Now select the ball called Interactable that is in the Scene. On the XR Grab Interactable script, change its layer mask to be Interactable.

Now when you start up the scene, you should notice that the LeftHand Controller can interact with the Interactable ball, but not the default ball.

This application is incredibly helpful to avoid unwanted interactions between the player and certain objects that you don’t want them to interact with.

4: Hide Controller on Select

To use this normally, all you need to do is click the box and make sure that the XR Controller Script on the left or right hand controller is instantiating the hand prefab found here.

The only problem is that we have a new Hand script that both animates our and instantiates it. If we choose to use this script, checking the Hide Controller On Select option will do nothing. To fix that, we’ll need to add a few things to our hand script.

Open up the Hand script and add the following to the top of the script

  • public bool hideHandOnSelect
  • private SkinnedMeshRenderer _handMesh
These variables will store if we want to hide our hand or not and the skin renderer will allow us to hide thing if we do. we also need to add the following function.

This will check if our hideHandOnSelect is set to true and if it is, it will toggle between the states of being enabled or not.

Last we need to add one line to our Initialize Hand Function.

Once the hand is instantiated, this will grab the component needed for our SkinnedMeshRenderer.

Now we just need to hook things up in the editor and we’ll be ready to test this out.

Back in the editor, navigate to the Hand Script and check the box to enable hide hand on select.

Now navigate to the XR Direct Interactor, drop down the Interactor Events and locate the event called Select Entered. Click the plus sign and drag the Hand Script into the empty slot. Drop down the menu and locate the Hand -> HideHandOnSelect function. Repeat this step for Select Exited. 

Repeat these steps for the LeftHand Controller and start up the Scene. Both controllers will hide the hand when you select something.

5 : Audio and Haptics

Letting the player know when something is occurring is incredibly important in any game or simulation. Constant feedback allows players to know what they can and can’t do or if something has happened at all. To add small cues for our users, lets add Audio and Haptic feedback.

Navigating the to XR Direct Interactor script you’ll see the Audio and Haptic events. The XR Interaction Toolkit is nice enough to give use multiple events to choose from.  The main ones we’ll focus on here are the Hover Entered and Select Entered.

Check the box for Audio Events -> On Select Entered. Click the Audio folder inside Assets and drag over the popselect sound onto the empty slot.

Now whenever we select an object, a little pop sound will play!

Next we need to add Haptic cues. 

Click the box for Hover Entered. You’ll notice that there is a Duration which is in seconds and a Haptic Intensity. For this we’ll set the Insanity to 0.125 and the Duration to 0.5. 

Click the box for Select Entered and set its intensity to .5 and the Duration for 1.

I find that a slight nudge for when a player has found an interactable object and an even bigger signal for when they’ve selected it gives a good game feel to it. In a way, it’s trying to mimic brushing an object vs grabbing onto it. There is way more of a tactile response when we grab something. 

Finished!

With that, you should have a better understanding of XR Direct Interactors, how to make them and what we can change with their attributes to make them better. Next on the chopping block will be XR Ray Interactors. Hope to see you there!