Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question GetButtonDown for XR in it's own function?

Discussion in 'VR' started by zacharygough07, Nov 19, 2022.

  1. zacharygough07


    Sep 11, 2021
    I am new to VR/XR development, so please go easy on me :)

    I am trying to use something similar to `Input.GetButtonDown()` for XR. I understand that you can get the state of a button like this: `targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool clicked)`, so I tried to modify a script from to give me an easy way to do it. What I have so far looks like this:

    Code (CSharp):
    1. bool GetXRButtonDown(InputFeatureUsage<bool> button)
    2.         {
    3.             if (targetDevice.TryGetFeatureValue(button, out bool clicked))
    4.             {
    5.                 bool isDown = false;
    6.                 // Is now clicked, was not clicked last frame, this is the down event
    7.                 if (clicked & !isDown)
    8.                 {
    9.                     return clicked;
    10.                 }
    12.                 isDown = clicked;
    14.                 return clicked;
    15.             }
    16.             else return false;
    17.         }
    So that I can call it just like this:
    if (GetXRButtonDown(targetDevice, CommonUsages.primaryButton))

    However, because the bool isDown is assigned as false when we call the boolean function, it cancels itself out. For this to work, I'd have to define the boolean isDown OUTSIDE of GetXRButtonDown, which can get very cluttered very quickly. I have also tried to put this boolean function in it's own public static script, but to no avail. So my overall question, no matter how I need to do it, I suppose, is: How can I GetButtonDown for XR Controllers? I would prefer it to be compact.
  2. Gozum


    May 18, 2020
    try to use new Input System, you can easily get the button down actions.