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.

Vive controller trigger press?

Discussion in 'AR/VR (XR) Discussion' started by dmennenoh, Oct 11, 2018.

  1. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    Could someone please help me out with this... How to get button (trigger) input from a Vive controller in the latest Unity plugin? All of the questions / answers I have found keep referencing the Player prefab in Steam - which does not seem to exist in the latest version.
    Also I have had a tough time finding any good tutorials on the new interaction system.
     
  2. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    OK, I found the Player prefab but I don't see anything in the HMD now - although is still shows as tracking in the scene. I'd rather use the old [CameraRig] as I don't need all the Hand stuff... any hints much appreciated.
     
  3. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    Really could use a hand here. I'm using the latest SteamVR plugin - 2.0.1
    In a brand new project I placed the CameraRig prefab in (not player), let it create it's default action classes, and everything works - I can see the controller, look around, etc.
    But I just can't figure out how to know if the controller trigger is pressed. Every example I can find, and even my own prior work, uses SteamVR_Controller like:

    Code (CSharp):
    1. private SteamVR_Controller.Device controller
    2.     {
    3.  
    4.         get
    5.         {
    6.             return SteamVR_Controller.Input((int)trackedObj.index);
    7.  
    8.         }
    9.  
    10.     }
    But SteamVR_Controller is no longer. I can't determine what has replaced it. Also, now the Controllers have SteamVR Behavior Pose scripts attached and not SteamVR Tracked Object. Just looking for a good example...
     
    BrainSlugs83 likes this.
  4. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    654
    Steam updated the plugin. Now they are using an action(event) system for the inputs. They did this so that player and dev can map their own button etc., similar to Unity Input system.

    When you import you have to go through the setup and generate data (Window>SteamVR Input> Save and generate) Take a look at the documentations(there 3 pdf's, two in root folder and one in InteractionSystem folder) they explain it all. I highly recommend, from my own experience, you don't proceed without understanding how everything works, or you will keep running into problems

    Here's a simple example:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Valve.VR;
    5.  
    6. public class InputTester : MonoBehaviour
    7. {
    8.  
    9.     public SteamVR_Action_Boolean grabPinch; //Grab Pinch is the trigger, select from inspecter
    10.     public SteamVR_Input_Sources inputSource = SteamVR_Input_Sources.Any;//which controller
    11.     // Use this for initialization
    12.  
    13.     void OnEnable ()
    14.     {
    15.         if (grabPinch != null)
    16.         {
    17.             grabPinch.AddOnChangeListener(OnTriggerPressedOrReleased, inputSource);
    18.         }
    19.     }
    20.  
    21.  
    22.     private void OnDisable()
    23.     {
    24.         if (grabPinch != null)
    25.         {
    26.             grabPinch.RemoveOnChangeListener(OnTriggerPressedOrReleased, inputSource);
    27.         }
    28.     }
    29.  
    30.  
    31.     private void OnTriggerPressedOrReleased(SteamVR_Action_In action_In)
    32.     {
    33.  
    34.         Debug.Log("Trigger was pressed or released");
    35.     }
    36. }
     
    sunwangshu, HorusVision and dmennenoh like this.
  5. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
  6. jshin

    jshin

    Joined:
    Oct 22, 2018
    Posts:
    1
    Hi. This helped me a lot as well :D. But can you explain the difference between using AddOnChangeListener compared to following code? Thanks

    private void Update()
    {
    if(SteamVR_Input.__actions_default_in_GrabPinch.GetStateDown(inputSource))
    {
    Debug.Log("Trigger was pressed");
    }
    }
     
  7. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    654
    AddOnChangeListener is an event. You can subscribe to it and whenever this gets triggered (when the corresponding button is pressed) it will call the method you subscribed.

    Your code simply checks to see if that condition (has the trigger button been pressed) is true on every frame.

    Another way to look at is event will tell you (Script that is interested), on a need to know basis, when something has happened, versus having to go and check every frame to see if something was done.

    If you are not familiar with events, take a look at these tutorials: https://unity3d.com/learn/tutorials/topics/scripting/events
     
  8. oscar_e_c

    oscar_e_c

    Joined:
    Dec 15, 2016
    Posts:
    5
    Thank you for this, i was running around in circles for hours trying to understand what was going on and when the changes were made. I'm used to developing for the rift using their sdk and haven't seen steamvr in a while.

    @FlightCrazed I created a new script, and copy/pasted your entire syntax and couldn't get a peep from my controller.
    I then copied in @jshin 's solution and finally was able to register the trigger!

    any thoughts on why I couldn't get yours to work? I don't believe it matters where the script is attached as long as its run, here i attached it to the Controller (right).

    I looked through the PDF's which helped to describe the approach, but little code to dissect... and the interactive sample scene is too complex for me to follow. The functionality i'm looking for is trapped somewhere in the longbow game... but all that knocking logic muddy's the water.

    I have a canon, i want to fire a projectile when the button is pressed, the projectile should de-parent upon firing and let physics take over. Seems like a simple thing... i could do it quickly with the rift... but my rift just died so now i'm scrambling to put together a solution with a borrowed Vive.

    help me obi-wan, you're my only hope!
     
    FlightOfOne likes this.
  9. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    654
    Did you update SteamVR asset from Steam to a later version? I recently updated(to fix some bugs) and noticed that they had changed event.

    If you did that, this is how the subscribing method should now look:

    Code (CSharp):
    1.  private void VRController_OnInteract_ButtonPressed(SteamVR_Action_Boolean action, SteamVR_Input_Sources sources, bool isConnected)
    2.         {
    3.            // Debug.Log("Interact");
    4.             OnInteractButton();
    5.         }
    P.S. Haha... I am more of Star Trek fan but I can go Star wars for today.
     
  10. ireth_86

    ireth_86

    Joined:
    May 18, 2016
    Posts:
    23
    Hi,
    I'm also trying to understand the 2.0.1 activities stuff.

    What I don't get is: where can I find the controller that is connected to the activity?
    If an event for the activity is raised, I expect to find the relative controller as a field of the parameter of the event, or something like that (especially when actions are mirrored and I'm not supposed to know which controller raised the event).

    Thanks in advance!
     
  11. jjenningsevox

    jjenningsevox

    Joined:
    Oct 20, 2016
    Posts:
    3

    Thank you so much , like the others have said this has helped a ton and saved me a few days of stress . The last issue I am having is finding a way for these events to reference the two dimensional axis for the vives Touchpad or the controller on the other remotes. Does anyone have an idea on how to read the touchpad or controller 2D axis using this event system ?

    I've tried changing the event type to AddOnActiveChangeListener , AddOnUpdateListener , and the AddOnChangeListener like those used on the example ( which are working perfectly fine for touchpad and trigger inputs) . I even tried getting a reference to the hand controls in the update loop using the following code and building off of the buggy and platformer actions bases and it didn't work :

    Code (CSharp):
    1.  private void Update()
    2.     {
    3.         SteamVR_Input_Sources hand = inputSourceMoveL;
    4.         Vector2 steer = Vector2.zero;
    5.  
    6.         steer = action_move.GetAxis(hand);
    7.  
    8.  
    9.         Vector2 m = SteamVR_Input.buggy.inActions.Steering.GetAxis(inputSourceMoveR);
    10.  
    11.         Debug.Log(" update ::: vector axis == " + m.ToString());
    12.  
    13.     }
    I feel like the crux of my issue comes when referencing the input source state and getting a current reference of the hands and any touches on the touchpad. any help at all is appreciated !
     
  12. ireth_86

    ireth_86

    Joined:
    May 18, 2016
    Posts:
    23
    I believe the simplest way to achieve that result is to use a Vector2 Action, and get the SteamVR_Action_Vector2.GetAxis() value inside some callback or Update() function.
     
  13. jjenningsevox

    jjenningsevox

    Joined:
    Oct 20, 2016
    Posts:
    3

    Hmm thank you for this but when I give that a try I get nothing when I try to get my logs to output something using this . I'm trying to use the default buggy steering and platformer move actions but those aren't working for me . It's almost like it's not registering the touchpad outside of the demo scene .

    This was how you suggested I implement it right ?

    Code (CSharp):
    1. void Update () {
    2.  
    3.  
    4.         Debug.Log( "action move 1 axis == " + action_move1.GetAxis(inputSourceMove1).ToString());
    5.         Debug.Log("action move 2 axis == " + action_move2.GetAxis(inputSourceMove1).ToString());
    6.  
    7.         Debug.Log("action move 1 axis == " + action_move1.GetAxis(inputSourceMove2).ToString());
    8.         Debug.Log("action move 2 axis == " + action_move2.GetAxis(inputSourceMove2).ToString());
    9.  
    10.  
    11.    
    12.     }
    13.  

    EDIT: OK I've found the issue when using the actions I have to call Activate primary on the existingactions events . So here I am declaring the actions themselves( the buggy and platformer events) and then on OnEnable I am calling SteamVR_ActionSet.ActivatePrimary to activate those specific bindings .

    Code (CSharp):
    1.  
    2.  public SteamVR_ActionSet actionSetPlatformer;
    3.     public SteamVR_ActionSet actionSetBuggy;
    4.  
    5. void OnEnable()
    6.     {
    7.         actionSetPlatformer.ActivatePrimary();
    8.         actionSetBuggy.ActivatePrimary();
    9.     }
    @ireth_86 Thank you so much for your help !
     
    Last edited: Dec 17, 2018
    ireth_86 likes this.
  14. ireth_86

    ireth_86

    Joined:
    May 18, 2016
    Posts:
    23
    I'm glad it helped.
    But I don't know if you can activate more than one Action Set at a time: I'm trying to do it right now, and I can see from the SteamVR Input Live View (you can find it under Window tab) that only one action set is active. In your case, it should be actionSetBuggy, since it's the last one you've activated through .ActivatePrimary().
     
  15. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    Every bit of code here has errors.... Did they make more changes?
     
  16. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    Even Vives code in their manual has errors...

    Code (CSharp):
    1.  public SteamVR_Input_Sources thisHand;
    2.  
    3.     private void Update()
    4.     {
    5.         if (SteamVR_Input._default.inActions.GrabPinch.GetStateUp(thisHand))
    6.         {
    7.             Debug.Log("grab pressed");
    8.         }
    SteamVR_Input does not contain a definition for _default...
     
  17. TeamSoftware

    TeamSoftware

    Joined:
    Mar 17, 2017
    Posts:
    2
    The "_default" in this case is a generated ActionSet. Its included with SteamVR, but you have to generate first in "Window->SteamVR Input". Where you can also define custom Sets and remap the Buttons via the new SteamVR Binding UI.

    If you dont generate the input classes, it will throw errors, because it cant find the defined ActionSets.

    Edit:

    After updating to 2.2 i see they really changed stuff a lot. The example from the Interaction pdf doesnt work, but heres a better approach, pretty much the same as above:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Valve.VR;
    4.  
    5. public class InputTest: MonoBehaviour
    6. {
    7.     public SteamVR_Action_Boolean TriggerClick;
    8.     private SteamVR_Input_Sources inputSource;
    9.  
    10.     private void Start() {} //Monobehaviours without a Start function cannot be disabled in Editor, just FYI
    11.  
    12.     private void OnEnable()
    13.     {
    14.         TriggerClick.AddOnStateDownListener(Press, inputSource);
    15.     }
    16.  
    17.     private void OnDisable()
    18.     {
    19.         TriggerClick.RemoveOnStateDownListener(Press, inputSource);
    20.     }
    21.  
    22.     private void Press(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
    23.     {
    24.         //put your stuff here
    25.         print("Success");
    26.     }
    27. }
     
    Last edited: Feb 14, 2019
  18. Electricpuppets

    Electricpuppets

    Joined:
    Apr 14, 2018
    Posts:
    4
    This worked...why...was this so hard to find...?

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Valve.VR;

    public class ShowTriggerValues : MonoBehaviour
    {
    public SteamVR_Input_Sources LeftInputSource = SteamVR_Input_Sources.LeftHand;
    public SteamVR_Input_Sources RightInputSource = SteamVR_Input_Sources.RightHand;
    private void Update()
    {
    Debug.Log("Left Trigger value:" + SteamVR_Actions._default.Squeeze.GetAxis(LeftInputSource).ToString());
    Debug.Log("Right Trigger value:" + SteamVR_Actions._default.Squeeze.GetAxis(RightInputSource).ToString());
    }

    }
     
  19. sunwangshu

    sunwangshu

    Joined:
    Mar 2, 2017
    Posts:
    18
    Thanks so much! This worked like a breeze. I spent hours trying to find it through examples, glad I finally found it here.
     
    may883520zxm likes this.
  20. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    654
    Let's hope we don't have to do this for much longer, when it finally starts supporting the new input system!
     
  21. may883520zxm

    may883520zxm

    Joined:
    Nov 25, 2022
    Posts:
    2
    Hello, when you say grab pinch from the inspector, I would like to know which inspector describes this information?
    And I also want to know that how can I get the controller like I have left and right controller, I only want to do something after the user pressed the trigger on the right hand? I tried a lot to get the input device, but I failed. There is no SteamVR_Controller in the 2.2 version