In this tutorial, I’m going to teach you how to get different input data from your VR controllers so you can utilize it in your games. This data can be used to add up a score like in Beat Saber, which uses the angle of your cut or in Thrill of the Fight, which uses the velocity of your controller to help determine how hard you’ve punched.
We’ll go over how we can look at controller data in the XR Interaction Debugger, what we need to do to access data from our input devices and how we can use that data to do things in our games.
The Template and Supplemental Materials
Using the Xr Interaction Debugger
In order to understand what kind of data our controllers and hmd collect, we should start by looking at the XR Interaction Debugger. To access it, do the following.
- Windows -> Analysis -> XR Interaction Debugger
- In the top left corner, select Input Devices and unclick Interactor and Interactables
Getting Input Data From Controllers
Seeing all that data is good, but being able to use it would be better!
In order to access our controllers and hmd, we first need to fetch them in a script. Opening up the script called InputData, we’ll see this.
Let’s see what this script does.
- We use the namespace UnityEngine.XR so we have access to classes such as InputDevice and InputDeviceCharacteristics.
- We create three public InputDevices that will store our left, right and hmd.
- Update() we constantly check if any of our InputDevices are not valid. If they are not, we try to initialize them again.
- InitializeInputDevices() will go through all our input devices and try to initialize them.
- InitializeInputDevice() will take InputDeviceCharactestics and try to populate whatever input device is passed into it if it’s able to find a device with those characteristics.
- For example : The first call to this function we pass in the characteristics of a controller and right. If it’s able to find a device that is a right controller, it will then store it in the variable passed in, which is our public variable called rightContoller.
So now that we’ve acquired our input devices, what can we do with them?
Using Data from Input Devices
From the folder we imported, drag and drop the prefab called DataCanvas.
Notice that in the components that make up this prefab, we have the InputData script and a new script called DisplayInputData. Expanding the DataCanvas and BottomLabel, we can also see that we have a references to the LeftScore and RightScore to the script.
Lets dive into the DisplayInputData script.
- Import UnityEngine.XR so we can use InputData class
- Import TMPro so we can use TextMeshProUGUI class
- RequireComponent is used to make sure an InputData script is attached as a component to the same object that this script is on.
- Two public variables used to store the TextMeshProUGUI for our left/right score display.
- private variable used to store a reference to our InputData script
- Two private floats to store the Max score of our left/right controller
- Start() populates our _inputData variable.
- Update()
- First we try to get the deviceVelocity from our leftController. If something valid is returned, it will store it as a Vectore3 called leftVelocity.
- Once a valid value is returned, it will then determine if its current magnitude is greater than the current _leftMaxScore.
- It then displays what the current max score is for the left controller.
To summarize this in shorter terms, this function simply gets the current velocity of each controller and checks if its overall magnitude is greater than any movement the player has done before.
Although this example is simple, we can see how a small input like this can lead to fun combinations when constructing a scoring system in a game. This is also only one piece of data out of multiple provided by our controllers and head mounted display.
Conclusion
These data points can be used in combination to your game to produce scoring like in Beat Saber or punch power like in Thrill of the Fight. How we can use this data is only limited by our imagination and desire to experiment. I hope you found this useful, and I’ll see you in the next one!