Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chris-hatton/c1f7b3a344f89a3e78c9346b345ef25e to your computer and use it in GitHub Desktop.
Save chris-hatton/c1f7b3a344f89a3e78c9346b345ef25e to your computer and use it in GitHub Desktop.
Fragment to get pressed D-Pad button from SteamVR controller in Unity
static class SteamVR_Controller_Extension
{
const float threshold = 0.3f;
/*
* You might expect that pressing one of the edges of the SteamVR controller touchpad could
* be detected with a call to device.GetPress( EVRButtonId.k_EButton_DPad_* ), but currently this always returns false.
* Not sure whether this is SteamVR's design intent, not yet implemented, or a bug.
* The expected behaviour can be achieved by detecting overall Touchpad press, with Touch-Axis comparison to an edge threshold.
*/
public static bool GetDPadPress( this SteamVR_Controller.Device device, EVRButtonId dPadButtonId )
{
if ( device.GetPress( SteamVR_Controller.ButtonMask.Touchpad ) ) // Is any DPad button pressed?
{
var touchpadAxis = device.GetAxis( EVRButtonId.k_EButton_SteamVR_Touchpad );
if ( touchpadAxis.y > (1.0f - threshold ) ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Up; }
else if ( touchpadAxis.y < threshold ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Down; }
else if ( touchpadAxis.x > (1.0f - threshold ) ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Right; }
else if ( touchpadAxis.x < threshold ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Left; }
}
return false;
}
}
@applesaucesome
Copy link

Oh, also, if you wanted to make it so it captures the press once the user releases the button, instead of currently where it sends a constant stream if the button is held down, you can update your if() statement for device.GetPress()instad to be device.GetPressUp()

Sorry, I'll stop spamming your wall now, thanks again! (I made sure to credit your gist in my source)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment