Search Unity

Get basic input from XR controller

Discussion in 'AR/VR (XR) Discussion' started by MattESqr, Aug 21, 2020.

  1. MattESqr

    MattESqr

    Joined:
    Feb 5, 2020
    Posts:
    15
    Hi , is there a straightforward way to capture a button event from an XR controller (in this case Oculus Rift)?

    Looking here - https://docs.unity3d.com/Manual/xr_input.html , this seems over the top to get the event we're after. I understand that this level of code is needed, but this feels like a lot of boilerplate code just to get back to where we were for years with other input systems. Shouldn't this stuff be abstracted for us by Unity?

    eg)
    Input.GetButton("Fire"); ...which would work with keyboard or controller depending on the mapping.

    Is there something similar to the above for XR input?

    Thanks!
     
  2. Discmage

    Discmage

    Joined:
    Aug 11, 2014
    Posts:
    60
    I have been looking at this most of today...and I agree that the new input system is WAY overcomplicated for the simplest of things. I can appreciate that the new Input system is more robust and can do more stuff (probably), but if I have to spend 3 hours trying to get a simple log for the A button on the left motion controller on my rift, then they aren't doing it right.

    That said, I still haven't figured out how to get that trigger. Previously, two lines of code, maybe three. Now...Well now I don't even know yet. Still working on it. You aren't alone in your frustration though.
     
  3. Discmage

    Discmage

    Joined:
    Aug 11, 2014
    Posts:
    60
    Using the 'latest' input system 1.0 which I installed from package manager (why they didn't rename it for search purposes (is it the old system or the new) I have managed to get gamepad and keyboard interaction easily enough as a last ditch effort to get ANY interaction.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class DoTheThing : MonoBehaviour
    7. {
    8.     private void Update() {
    9.  
    10.         if (Gamepad.current.aButton.isPressed) {
    11.             Debug.Log("A is pressed!");
    12.         }
    13.  
    14.     }
    15. }
    So if anyone would kindly point out the XR equivalent of this that would probably be all I/we need.
     
  4. Discmage

    Discmage

    Joined:
    Aug 11, 2014
    Posts:
    60
    Just putting this here because for some reason this was hard to find easily in documentation and forums...maybe it's just me, but still. Credit to a friend for getting this code to me.

    This is using the XR Integration Toolkit...NOT an input manager.

    Also, If you don't have the headset ON, the boolean is not triggered. So while it was working with no errors, it won't actually 'work'. I was not getting any input from the controllers because I did not have the headset on (I assume it's the sensor though)...This took me a while to figure out though as I assumed I wouldn't have to have the headset on to test just the motion controllers.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.Interaction.Toolkit;
    5.  
    6. public class ControllerManager : MonoBehaviour
    7. {
    8.     public XRController rightHand;
    9.     public InputHelpers.Button button;
    10.  
    11.     void Update() {
    12.  
    13.         bool pressed;
    14.         leftHand.inputDevice.IsPressed(button, out pressed);
    15.  
    16.         if (pressed) {
    17.             Debug.Log("Hello - " + button);
    18.         }
    19.     }
    20. }
    Place this on the XRRig, drag in the motioncrontroller to the XRCoontroller variable and Voila! It also has access to all the other buttons too. SO it's pretty good...now that I've found it AND figured out the fact I have to have the headset on.
     
    Nikud likes this.
  5. Mboe94

    Mboe94

    Joined:
    Mar 28, 2021
    Posts:
    3

    Damn, you almost made my day. However can't move my controllers into the script so doesn't work. Been wasting past 3-4 days trying to figure this out - what a waste of time. Why not just GetKeyDown and then primaryButton or something? Seriously wtf is this? This is so discouraging. If something as simple as a debug.log when pressing a button is this hard then it's not for me. Was a fun week trying to program a bit though. GL to the rest of you.
     
    bindon likes this.
  6. Nikud

    Nikud

    Joined:
    Dec 10, 2019
    Posts:
    2
    Hey there, for me using the action-based system the code by Discmage did not work (or maybe I did something wrong). But I found this solution for getting e.g. the trigger button:

    Code (CSharp):
    1.             if(currentInteractor.GetComponent<ActionBasedController>().activateAction.action.ReadValue<float>() > 0.5f)
    2.             {
    3.                 Debug.Log("trigger pressed");
    4.             }
     
  7. NickSanches

    NickSanches

    Joined:
    Mar 13, 2020
    Posts:
    1
    How did you get the currentInteractor to work? I am busy working on my first VR game and don't have much experience with Unity. I want to reload a gun by pressing the primary button of the controller that's currently holding the gun. I cannot seem to find anything closely resembling your code when I look for it online.
     
  8. guptadevang

    guptadevang

    Joined:
    Feb 26, 2024
    Posts:
    1
    Hey, I have been working on a game which has a in-game slider to alter the movement speed of the player. And I am using the XR Locomotion System. I cannot find a syntax to read or alter the Move Speed in Continuous Move Provider (Action-based).

    I tried to use:
    [SerializerField] private ActionBasedContinuousMoveProvider.MoveSpeed moveSpeed;
    but it is not working.

    By the way this below is executing without any syntax errors but I don't to how to read the move speed value.
    [SerializerField] private ActionBasedContinuousMoveProvider moveSpeed;
     
  9. abhijiiitpatil

    abhijiiitpatil

    Joined:
    Feb 27, 2024
    Posts:
    3
    I don't think that it is possible to alter movement speed of the player using the XR locomotion system you need to write your custom script for it i think :)