Search Unity

Vive controller trigger only once

Discussion in 'AR/VR (XR) Discussion' started by pippolo, May 20, 2020.

  1. pippolo

    pippolo

    Joined:
    Jul 18, 2017
    Posts:
    19
    Hello I have the following InputManager for my vive controller.
    In my program at the moment, when the user pulls the trigger it changes to a new scene. The problem is that if the user doesn't release the trigger it can basically skip through all the scenes (because IsPressed() always returns true). I was wondering if there's a built in method that can only be called the first time you pull the trigger. After that you're forced to release the trigger or the method won't be called again. Hope the explanation is clear. Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using System.IO;
    6. using Valve.VR;
    7.  
    8. public class VRInputManager : MonoBehaviour
    9. {
    10.     public SteamVR_Action_Boolean buttonPress;
    11.     public SteamVR_Input_Sources handType;
    12.     public SteamVR_Action_Pose pose;
    13.     private bool pressed;
    14.  
    15.     private void Awake()
    16.     {
    17.         pressed = false;
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.         buttonPress.AddOnStateDownListener(TriggerDown, handType);
    23.     }
    24.  
    25.     public void TriggerDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
    26.     {
    27.         //Debug.Log("TRIGGER PRESSED!");
    28.         pressed = true;
    29.     }
    30.  
    31.     public float GetXPos()
    32.     {
    33.         return Mathf.Clamp(Utils.Remap(pose.localPosition.x, -0.20f, 0.20f, -1, 1), -1, 1); ;
    34.     }
    35.  
    36.     public bool IsPressed()
    37.     {
    38.         return pressed;
    39.     }
    40. }
    41.