Search Unity

Issues Distinguishing between 2 Vive controllers in C#

Discussion in 'Scripting' started by kabirverman, May 4, 2018.

  1. kabirverman

    kabirverman

    Joined:
    Apr 19, 2018
    Posts:
    1
    I'm having issues distinguishing between Vive Controllers in my script, in the following code, when I grip the left controller the Debug log fires for both left and right controllers. When i grip the right controller, nothing happens. I have the controllers linked up to the script in the inspector and I'm really confused as to what it is I'm doing wrong here. I'd really appreciate anyone's help!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Grips : MonoBehaviour {
    6.  
    7.     public SteamVR_TrackedObject lHandControllerObject;
    8.     public SteamVR_Controller.Device lHandControllerDevice;
    9.  
    10.     public SteamVR_TrackedObject rHandControllerObject;
    11.     public SteamVR_Controller.Device rHandControllerDevice;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.          lHandControllerObject = GetComponent<SteamVR_TrackedObject>();
    17.          rHandControllerObject = GetComponent<SteamVR_TrackedObject>();
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         GripPress();
    23.     }
    24.  
    25.  
    26.     void GripPress()
    27.     {
    28.         lHandControllerDevice = SteamVR_Controller.Input((int)lHandControllerObject.index);
    29.         rHandControllerDevice = SteamVR_Controller.Input((int)rHandControllerObject.index);
    30.  
    31.         if (lHandControllerDevice.GetPress(SteamVR_Controller.ButtonMask.Grip))
    32.         {
    33.             Debug.Log("gripped left");      
    34.         }
    35.      
    36.  
    37.         if (rHandControllerDevice.GetPress(SteamVR_Controller.ButtonMask.Grip))
    38.         {
    39.             Debug.Log("gripped right");
    40.         }
    41.  
    42.     }
    43.  
    44.  
    45. }
    46.