Unity VR : Interactable Objects with Ray and Direct Interactors
The first things they implemented when they were working on the Oculus were hands and interactions. If the creators of a billion dollar company determined this was the most important thing to work on first, then it should be the first thing we work on too.
If you missed my tutorial on how to setup a VR project in Unity, you can find it here.
Step 1: Changing Hands
The previous tutorial I made some spheres to temporarily represent our hands. It’s time to get rid of those and replace them with some quick prototype hands that I made. You can download the hands prototypes here. Just download the repository, unzip it, and find the prefabs folder and drag it into your project. They aren’t the prettiest hands, but they’ll do!
Now locate and click on a LeftHand or RightHand Controller from your XR Origin within the Scene Hierarchy. Depending on which you’ve chosen, find the XR Controller (Action-Based) script. Navigate to Model Prefab and drag and drop the respective hand prefab onto that slot.
Repeat the above step for the other hand.
That’s it! You should have two things that look more like hands… kind of.
Step 2 : Add Interactable Objects
To start, add a cube to the project and mov it to position of (0, .5 , 1). If your XR Origin is set to a position of (0, 0, 0), this should put the table right in front of you when testing.
Then add a 3D object Sphere to the scene. Shrink the Scale down to (.2, .2, .2) so it will be the size of a ball and mov it on top of the cube you just made.
Now go over to the Inspector and click Add Component at the bottom. Look for XR Grab Interactable and add it to the object. Notice that it adds a Rigidbody to the object. The XR Grab Interactable requires you to have a Rigidbody on the object in order for it to work.
Now copy the ball twice and move them to side of the original ball. Put a different material on each of the new balls so we can tell the difference between each one.
Now choose one of the new balls and change its Movement Type to Kinematic under XR Grab Interactable. Choose the third ball and change its Movement Type to Velocity Tracking.
The reason we made three different balls was to showcase the difference in movement types.
Instantaneous – This type will move with your hand when you grab the object without any physics. It actually disables the Rigibody when you are holding the object so it will clip through anything.
Kinematic – This type will give a movement delay and be influenced by your hand’s physics. It is able to pass through objects that do not have a Rigidbody when held, but will collide with them when it is not held.
Velocity Tracking – This one also has a delay in tracking as it’s simulating force being added to a handheld object. When holding it, it will collide with all objects even if they do not have a Rigidbody. It’s great for simulating physics with a door or cabinet object or bowling ball.
Step 3: XR Ray Interactor
The great news about this step is that it’s already done! Both hands should already have this by default.
If you don’t have this attached to your hands, just click on the hand missing the component, find Add Component in the inspector and add XR Ray Interactor. It will add a Line Renderer and XR Interactor Line Visual script.
In order to use the XR Ray Interactor, just aim it at an object that is a XR Grab Interactable and press the grip key.
The XR Ray Interactor is great for working with objects from afar and teleporting (which I’ll cover later). I just wanted to mention 3 important variables in the script.
Force Grab – This will allow you to grab objects from far away. If you were going to use the Ray Interactor only for teleporting, you’d want to turn this one off.
Anchor Control – This allows you to move the object along the ray by using the thumbstick. It’s a pretty neat ability, but It typically isn’t the behavior you want for your game and I usually turn it off.
Hide Controller on Select – This is great to turn on when you don’t have the time to animate hands or properly anchor objects to the hand. I’ve even seen games that have had major success use this!
Step 4 : XR Direct Interactor
Most of us don’t shoot lasers out of our hands and grab thing from afar. That’s where the XR Direct Interactor comes in. To add it, do the following.
- Select RightHand Controller and remove the following components: XR Interactor Line Visual, Line Renderer, XR Ray Interactor.
- Click Add Component and add a XR Direct Interactor
- Click Add Component and add a Sphere Collider
- Set Sphere Collider to be a Trigger
- Set Sphere Collider radius to .1 (about the size of our hand)
That’s it! Your hand should now be able to directly interact with any object that has a XR Direct Interactable script on it.
Step 5 : Haptics and Audio
- VR is about immersion and one of the best ways to produce that is with haptics and audio. Connect the player to controllers with these tools is important and easy.
Both the XR Ray Interactor and XR Grab Interactor have settings to enable these features. Follow these steps to experiment to help immerse your player with Haptics and Audio.
For Audio
- Select your Controller with either a XR Grab Interactor on it or a XR Ray Interactor.
- Open drop down for Audio Events.
- Add any audio file to you project
- Choose On Select Entered under Audio Events
- Add the audio clip to your On Select Entered event
- Select your Controller with either a XR Grab Interactor on it or a XR Ray Interactor.
- Open drop down for Haptic Events
- Choose On Select Entered
- Set Haptic Intensity to .5
- Set Duration to .25
All Set!
That’s it for now! I’ll add another post following this one to go over how to attach objects a certain way to your hand so it doesn’t look like it is going through them. Cheers!