Any way to get OnButtonDown with XRControllers?

This is my current setup:

if(device.TryGetFeatureValue(CommonUsages.secondaryButton, out bool isPressed))

This is linked to a canvas gameobject to be set active / unactive but it seems every frame it will be set unactive and then active and whatever it lands on when you release the button is the result. What should I do?

You should keep track yourself if the button was down on the previous frame, and then change your if-logic to “if the button is pressed now and was not pressed last frame”.

Hmm okay that makes sense, so I take it something like this?:

if(!isPressed)
{
    if(isPressed)
    {

    }
}

No, that would never me true. Something like this (pseudocode)

    device.TryGetFeatureValue(CommonUsages.secondaryButton, out bool isPressed);
    if (isPressed && !wasPressed) {
        // button was *just* pressed, so do stuff
    }
    wasPressed = isPressed;

Cheers I will give it a go now tell you if it works

Thanks it works perfectly. I had to define wasPressed as a private bool outside of the function though. Not that big of a deal

1 Like