How To : Unity VR Basics 2023 – Ray Interactions

 In this guide we’ll learn how to build an XR Ray Interactor, what attributes it has, how to grab objects from far away and how to create UI for VR and interact with it using our Ray Interactors.

XR Interaction Toolkit 2.4!

Unity has released the 2.4 version of the XR Interaction Toolkit and with it comes some improvements to the XR Ray!

As we can see, they’ve added a few performance and visual improvements to our XR Rays! In order to use this, we’ll need to update our current version of the XR Interaction Toolkit and also confirm that we’ve installed the the Burst package.

To update the XR Interaction Toolkit, do the following.

  •  Window -> Package Manager
    • Select the top left menu plus menu and choose ‘Add Package By Name’

 

    • for the name, use ‘com.unity.xr.interaction.toolkit’
    •  Select the XR Interaction Toolkit package
      •  Select Samples and update the Starter Assets
Last, we just need to make sure that the Burst package is installed. We can do that through the Package Manager window and selecting Packages In Project to display all the packages installed in this current project. If the Burst package is not installed, we can search the Unity Registry and add it to the project.
With that, we’re ready to set up our XR Ray Interactors!

Moving the XR Grab Interactable

If you’re following along from the previous tutorial, then we’re going to need to move the Direct Interactor. This will help us keep things organized in future tutorials.

  • Select the LeftHand Controller from the XR Origin
    • Create a XR Direct Interactor and select it
      • Remove the XR Controller component from the Direct Interactor
    • Select the LeftHand Controller
      • Remover the Sphere Collider and XR Direct Interactor

If we start up the scene, our Grip controls should still work, but now we can add more interactive components in an organized way! Keeping these compartmentalized will help in the future when we have to activate/disable certain Interactables depending on the player’s input.

XR RAY INTERACTOR SETUP

Just like the XR Direct Interactor, we need to make a Ray Interactor that is a child of the Left/Right Controller objects.

  • Right click Left/Right Controller and add go to XR -> Ray Interactor (Action-Based)
    • Name it Ray Interactor
    • Select the the Ray Interactor
      •  Remove the XR Controller (Action-based) component
Since we already have the XR Controller component on the Left/Right Controller parent object, we won’t need it for the child object.

 

If we were to start up the Scene now that had XR Grab Interactables in it, we would be able to grab them! Now that the XR Ray Interactor is set up, let’s go over it’s attributes.

XR Ray Interactor Attributes

  • Interaction Manager – This manager is in charge of all interactions between the interactable object and the XR Ray Interactor. If there is one in the scene, the XR Ray 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
  • Enable Interaction with UI GameObjects – This will toggle whether we can interact with UI components.
  • Force Grab –  Toggles whether we can grab interactable objects from far away and pull them to us. When we turn this off, you can still grab objects from far away, but they will not snap to your hand position.
  • Anchor Control –  This will give you the ability to pull objects towards you or away from you using the thumbstick. Below it are a bunch of attributes that allow you to tweak this ability.
  • Attach Transform –  This is the Transform that an interactable object will attach to. If you don’t set one, it’ll use the XR Ray Interactor’s transform by default.
  • Ray Origin Transform –  This allows you to set the Origin of where you want your Ray to originate from.
  • Raycast Configuration –  There are three different types of lines we can use with the Raycast Configuration. The straight line is set by default and is great for UI and Object interaction. We also have a Projectile Curve which could be useful for showing possible trajectory of a launched object. Finally, we have the Bezier curve which is great for teleportation. 
  • Selection Configuration –  Much like Direct Interactors, this also has similar attributes that we can use for selection. 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).
  • 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.
  • Line Width – Sets the width of the line used for the Ray.
  • Width Curve –  Sets the width of the line from start to finish.
  • Valid Color Gradient – Sets the color of our ray when we hover over something that is a valid interactable.
  • Invalid Color Gradient –  Sets the color of our ray when we hover over something that is an invalid interactable.
  • Override Line Length –  Determine if you should set line length using the Line Visual script (true) or to have it be determined by the interactor (false). 
  • Stop Line At First Raycast Hit –  Set to true, it will stop the line from extending past the first object it hits. It will continue past the object if set to false.
  • Reticle – This will display a custom reticle at the end of our line when we hover over a valid object.
  • Auto Adjust Line Length –  Adjusts the length of the line to a shorter amount when the Ray Interactor does not encounter an object that is valid.
  • Line Bend Ratio –  Determines the amount the line bends  when moving objects around.

XR Transform Stabilizer

A new thing added to the XR Interaction Toolkit 2.4 is the XR Transform Stabilizer. This component uses the Burst compiler to help render our Rays in a more efficient manner with low overhead. We should add it to enjoy the new benefits!

  • Right click the Left/Right Controller and add an empty gameobject called ‘Left/Right Ray Stabalizer’
  • Select the newly created object
    • Add Component XR Transform Stabalizer
    • In the empty slot for Target, click and drag the Left/Right Controller gameObject

That’s it! We’ve added the new XR Transform Stabilizer!!!

CREATING A VR UI CANVAS

Ray Interactors have become the primary way to interact with UI for many games in the VR space. To get started, we’re going to need a UI to interact with in the first place.

Right click in the scene XR -> UI Canvas. This will add a Canvas and EventSystem to our Scene that is already configured for XR development. 

Now we need to position it in the world space so we can interact with it. Enter the following for the Canvas

  • Set position to (0, 1.6, 2)
  • Width: 1000 Height: 750
  • Scale down the Canvas to (.002, .002, .002).

This will reduce our Canvas to a good world space size and place it behind the table.

Lets add some quick components to the canvas just to make sure things are working when we start using the Ray Interactor on it. 

 

Double clicking the Canvas and switching the view to 2D mode will make it easier to work on the Canvas.

Next, we’re going to add some UI elements to our Canvas. Right click on the Canvas object in the scene and going to UI. From here you can choose any UI item you want to try out. I used 3 buttons and a Slider, but feel free to test out multiple UI elements.

If we start up the Scene now, we should be able to interact with the UI when we point at it!

 

Conclusion!

That’s it for the basics of XR Ray Interactors! Make sure to experiment with the settings and see what settings are right for your Ray Interactors!