2017-04-20 2 views
1

Ich möchte einfach nur Controller-Eingaben vom Benutzer in meinem VR-Spiel bekommen, und ich möchte auch das SteamVR Interaktionssystem verwenden, damit ich einfache UI-Sachen implementieren kann . Ich kann jedoch keine Eingabe vom Controller aus dem Hand-Skript erhalten.Wie bekomme ich Controller Input wenn ich das SteamVR Interaction System verwende?

Alles, was ich tat, war Ziehen in der "Player" Fertig, und schreiben Sie dann ein Skript auf das Hand-Objekt gehen, um die Eingabe von den Triggern zu erhalten.

private Hand _hand;      // The hand object 
    private SteamVR_Controller.Device controller; // the controller property of the hand 


    void Start() 
    { 
     // Get the hand componenet 
     _hand = GetComponent<Hand>(); 
     // Set the controller reference 
     controller = _hand.controller; 
    } 

    void Update() 
    { 
      // === NULL REFERENCE === // 
     if (controller.GetHairTrigger()) 
     { 
      Debug.Log ("Trigger"); 
     } 

    } 

Das gibt mir eine Nullref-Ausnahme für das Objekt "Controller". Ich habe auch versucht, die Controller-Komponente in OnEnable() und Awake() bekommen und das hat auch nicht funktioniert. Auch in Update(). Aus irgendeinem Grund enthält die SteamVR-Klasse Hand keinen Verweis auf den Controller. Mache ich etwas falsch? Fehle ich eine Art von Index-Spezifikation, wenn ich den Controller bekomme?

Ich bin in der Lage Reglereingang wie folgt zu erhalten:

private SteamVR_TrackedObject trackedObj;  // The tracked object that is the controller 

    private SteamVR_Controller.Device Controller // The device property that is the controller, so that we can tell what index we are on 
    { 
     get { return SteamVR_Controller.Input((int)trackedObj.index); } 
    } 

    private void Awake() 
    { 
     // Get the tracked object componenet 
     trackedObj = GetComponent<SteamVR_TrackedObject>(); 
    } 

    void Update() 
{ 
    if(Controller.GetHairTrigger()){ 
      Debug.Log("hey trigger"); 
    } 
    } 

Aber dann kann ich die Interaktion System verwenden. Jeder hat eine Ahnung?

Antwort

1

Ich würde vorschlagen, dass Sie wirklich Valves frei HTC.UnityPlugin oben auf SteamVR Integration hinzuzufügen, so können Sie schnell die Eingabe-Controller Zugriff mit

using UnityEngine; 
using HTC.UnityPlugin.Vive; 
public class YourClass : MonoBehaviour 
{ 
    private void Update() 
    { 
     // get trigger 
     if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Trigger)) 
     { 
      // ... 
     } 
    } 
} 
1

ohne zusätzliche Abhängigkeiten außer SteamVR zu installieren, schließen Sie das Skript eine der Hand Komponenten. Es ist ein Kind des Player Prefab im SteamVR Asset-Ordner.

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class Grab: MonoBehaviour { 
    private Valve.VR.InteractionSystem.Hand hand; 

    void Start() { 
     hand = gameObject.GetComponent<Valve.VR.InteractionSystem.Hand>(); 
    } 

    void Update() { 
     if (hand.controller == null) return; 

     // Change the ButtonMask to access other inputs 
     if (hand.controller.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) 
     { 
      Debug.Log("Trigger down") 
     } 

     if (hand.controller.GetPress(SteamVR_Controller.ButtonMask.Trigger)) 
     { 
      Debug.Log("Trigger still down") 
     } 

     if (hand.controller.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)) 
     { 
      Debug.Log("Trigger released") 
     } 
    } 
} 
Verwandte Themen