- Home /
VR: get controller velocity
Hey there,
I am currently loosing my mind about something that I thought would be super simple. I want to get the velocity vector (or position vector) of my Oculus Quest 2 controller so I can use it in my code. Currently I am trying to do this with XRNodeStates (https://docs.unity3d.com/ScriptReference/XR.XRNodeState.TryGetVelocity.html). However I just can't figure out a way how to make it work - I am a complete beginner sorry.
What would the code be for this? If you could help me out that would be great!
All the best, Yves
Answer by Reflekst · Apr 26 at 09:59 AM
Hey, It works for me. I found it there: https://www.reddit.com/r/learnVRdev/comments/n5e8tm/how_to_use_controller_velocity_in_unity_xr/
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;
public class ControllerVelocity : MonoBehaviour {
InputDevice LeftControllerDevice;
InputDevice RightControllerDevice;
Vector3 LeftControllerVelocity;
Vector3 RightControllerVelocity;
void Start()
{
LeftControllerDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
RightControllerDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
}
void Update()
{
UpdateInput();
}
UpdateInput()
{
LeftControllerDevice.TryGetFeatureValue(CommonUsages.deviceVelocity, out LeftControllerVelocity);
RightControllerDevice.TryGetFeatureValue(CommonUsages.deviceVelocity, out RightControllerVelocity);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613055812im_/https://answers.unity.com/themes/thub/images/avi.jpg)