How To : Unity VR Basics 2023 – Grabbing Objects

IT’s time to go over how to pick things up in VR. In order to do that with Unity and the XR Interaction Toolkit, we’re going to need two main components. 

A XR Direct Interactor and a XR Grab Interactable!

Adding a XR Direct Interactor

To add a XR Direct Interactor, we need to do the following.

  • Expand the XR Origin in the Scene Hierarchy
    •  Add a Empty Game Object to the Left/Right Hand Controllers
      • Name it ‘DirectInteractor’
      • Add Component XR Direct Interactor
      • Add Component Sphere Collider
        • Is Trigger = True
        • Radius = 0.1
      • Repeat these steps for the other Controller that is not set yet
With that, we’ve set up our controllers to grab interactable objects. Make sure to take careful notice that we added a Sphere Collider that is set to IsTrigger. Without having some form of collider that is a trigger, the XR Direct Interactor will not work. Let’s go over some of the attributes in the XR Direct Interactor.

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.
 

With that explanation out of the way, let’s get some game objects together so we can pick them up.

Making Interactable Objects

To make a grab interactable, we need to do the following.

  •  Make a Cube
    • Scale = (0.1, 0.1, 0.1)
    • Add Component XR Grab Interactable
Notice that this will also add a Rigidbody to the game object. This component is required for a grab interactable to work. I’ve placed mine on a table in front of the XR Origin so I can easily interact with it when I press play.

When we start up the Scene, we should be able to pick up the cube!

Next we need to cover movement types. To do that, we need to duplicate the cube 2 times and separate them. Locate the XR Grab Interactable component and set their movement types to Instantaneous, Kinematic and Velocity Tracking.

XR GRAB INTERACTABLE 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. 

Instantaneous


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. 

Kinematic


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.

Velocity Tracking

XR GRAB INTERACTABLE ATTRIBUTES

The XR Grab Interactable component doesn’t just stop at movement types when it comes to customization. Here I will list some important attributes that will help make you grabbable objects more fine tuned for whatever you’re building.

Interaction Layer Mask –  This can be used to assign your objects to different interaction layer masks. An example would be preventing an object from interacting with the player body causing unwanted collisions.

Colliders – These can be used as an array of colliders to add to our interactable game object if one collider is not enough.

Track Position –  This determines if an object should track the position of the thing grabbing it. You might want to turn this off if you were only interested in the rotation of the object relative to the hand grabbing it.

Smooth Position – When checked, this will allow you to smooth out the movement of the object in hand. This is great for simulating weight of an object.

Track Rotation –  This is great for  toggling on or off if you want to follow the hands rotation or ignore it. 

Smooth Rotation –  Just like smooth position, this will smooth out the rotation of the hand when played with.

Throw on Detach – This can help stop the object from being thrown if that isn’t desired from the player. If toggled on, you can change various attributes such as velocity scale to alter how the object acts one released from the player’s hand.

Attach Ease In Time –  When grabbing the  object, this will determine the time it takes to attach to your hand. It produces a very cool floating towards your hand over the set period of time.

Attach Points

Currently all our objects just attach to our hands in the center, which may not always be desired. Sometimes we want our objects to attach in a certain orientation. We can do this by using Attach Points.

To understand how to set up Anchor Points correctly, we need to look at the X(red), Y(green) and Z(blue) Axis. 

  • The Y Axis should be pointing up in the direction we want our object to point up. 
  • The X Axis should be pointing towards where we want the palm of our hand. 
  • The Z Axis we want to have facing in the direction we want our object to face.
 
 
 
 
 
 
 
 
 

Take note that this should all be based off of local rotation and not global.

Select the attach point and rotate it so it’s Z Axis faces forward, Y Axis points up, and X Axis faces towards where you want the palm of your hand to be.

Here is an example of how you would do it using a gun.

Last, we need to let the XR Grab Interactable script know that this is our attach point by assigning it.

Now our gun should attach like normal!

INTERACTABLE EVENTS

Providing constant feedback to the user is always recommended to help with immersion in VR. We can do this be using Interactable Events. These events occur through numerous stages of the XR Grab Interactable and the XR Direct Interactor.

On the XR Grab Interactable, we have the following events.

There are a ton of events to pick from ranging from hovering over an object to no longer selecting an option. This gives us a solid variety to have interactions with our objects that we’d expect. Maybe we want to change the mesh on the interactable object when we hover, so the player knows they can pick it up. 

If we select the XR Direct Interactor, we have the following.

Here you can see that we can have Audio Events and Haptic Events for our players. When they pick up bigger items, we can produce a stronger haptic feedback response. We also have Interactor Events where we can make function calls. Maybe we want to activate a particle effect every time the player picks up or drops an object.

With these small additions, our VR experience can grow exponentially in terms of immersion.

Interaction Layer Mask

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.

Conclusion

These is still a ton to cover and new additions to the XR Grab Interactable and XR Direct Interactor, but for now, that will have to do it. I’ll be covering multi-hand object grabbing in a future tutorial. Hope this helped and I’ll see you in the next one!!!!

1 thought on “How To : Unity VR Basics 2023 – Grabbing Objects”

  1. is there a difference between this module compared to your previous grab interaction session?

Comments are closed.