Search Unity

Oculus Go with Unity (2018) native VR?

Discussion in 'VR' started by R1PFake, Sep 10, 2018.

  1. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    540
    Does the Unity (2018) native VR integration already support the Oculus Go (including controller tracking/input) or does it currently require any plugins?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Mostly. Here's the code I use for controller tracking:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OculusVRTracking : MonoBehaviour {
    6.  
    7.     public Transform controller;
    8.    
    9.     public static bool leftHanded { get; private set; }
    10.  
    11.     void Awake() {
    12.         #if UNITY_EDITOR
    13.         leftHanded = false;        // (whichever you want to test here)
    14.         #else
    15.         leftHanded = OVRInput.GetControllerPositionTracked(OVRInput.Controller.LTouch);
    16.         #endif
    17.     }
    18.  
    19.     void Update() {
    20.         OVRInput.Controller c = leftHanded ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch;
    21.         if (OVRInput.GetControllerPositionTracked(c)) {
    22.             controller.localRotation = OVRInput.GetLocalControllerRotation(c);
    23.             controller.localPosition = OVRInput.GetLocalControllerPosition(c);
    24.         }
    25.     }
    26. }
    This does require OVRInput, which is part of the Oculus Utilities plug-in. However, I recommend simple code like the above rather than Oculus's sample code, which grossly overcomplicates it. With the above for tracking controllers, and the camera automatically tracking the head, you're just about set. The only other thing you might need is checking the various controller buttons; you can do that to some extent through the standard Input class, but to get things like touches on the thumb disc, you need to use OVRInput again. Here's how I do it (again, much simpler than the Oculus code makes it seem):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OculusInput : MonoBehaviour {
    6.  
    7.     // Virtual buttons which we adapt to both desktop and controller inputs:
    8.     public enum VButton {
    9.         Left = 0,
    10.         Right,
    11.         Up,
    12.         Down,
    13.         Trigger,
    14.         Back,
    15.         SwipeLeft,
    16.         SwipeRight,
    17.         SwipeUp,
    18.         SwipeDown
    19.     }
    20.     const int VButton_count = 10;
    21.  
    22.     bool[] currBtnState = new bool[VButton_count];
    23.     bool[] prevBtnState = new bool[VButton_count];
    24.  
    25.     Vector2 touchDownPos;
    26.     Vector2 touchUpPos;
    27.     float touchDownTime;
    28.    
    29.     static OculusInput _instance;
    30.     public static OculusInput instance { get { return _instance; } }
    31.    
    32.     void Awake() {
    33.         _instance = this;
    34.     }
    35.  
    36.     void Update() {
    37.         for (int i=0; i<VButton_count; i++) prevBtnState[i] = currBtnState[i];
    38.        
    39.         #if UNITY_EDITOR
    40.         bool shift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
    41.         currBtnState[(int)VButton.Left] = Input.GetKey(KeyCode.LeftArrow) && !shift;
    42.         currBtnState[(int)VButton.Right] = Input.GetKey(KeyCode.RightArrow) && !shift;
    43.         currBtnState[(int)VButton.Up] = Input.GetKey(KeyCode.UpArrow) && !shift;
    44.         currBtnState[(int)VButton.Down] = Input.GetKey(KeyCode.DownArrow) && !shift;
    45.         currBtnState[(int)VButton.Trigger] = Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0);
    46.         currBtnState[(int)VButton.Back] = Input.GetKey(KeyCode.Escape);      
    47.         currBtnState[(int)VButton.SwipeLeft] = Input.GetKey(KeyCode.LeftArrow) && shift;
    48.         currBtnState[(int)VButton.SwipeRight] = Input.GetKey(KeyCode.RightArrow) && shift;
    49.         currBtnState[(int)VButton.SwipeUp] = Input.GetKey(KeyCode.UpArrow) && shift;
    50.         currBtnState[(int)VButton.SwipeDown] = Input.GetKey(KeyCode.DownArrow) && shift;
    51.         #else
    52.         if (OVRInput.Get(OVRInput.Button.PrimaryTouchpad)) {
    53.             Vector2 pos = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad);
    54.             float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
    55.             Debug.Log("Touch angle: " + ang);
    56.             currBtnState[(int)VButton.Left] = (ang > 135 || ang < -135);
    57.             currBtnState[(int)VButton.Right] = (ang < 45 && ang > -45);
    58.             currBtnState[(int)VButton.Up] = (ang > 45 && ang < 135);
    59.             currBtnState[(int)VButton.Down] = (ang < -45 && ang > -135);
    60.         } else {
    61.             currBtnState[(int)VButton.Left] = false;
    62.             currBtnState[(int)VButton.Right] = false;
    63.             currBtnState[(int)VButton.Up] = false;
    64.             currBtnState[(int)VButton.Down] = false;
    65.             currBtnState[(int)VButton.SwipeLeft] = false;
    66.             currBtnState[(int)VButton.SwipeRight] = false;
    67.             currBtnState[(int)VButton.SwipeUp] = false;
    68.             currBtnState[(int)VButton.SwipeDown] = false;
    69.            
    70.             if (OVRInput.Get(OVRInput.Touch.PrimaryTouchpad)) {
    71.                 Vector2 pos = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad);
    72.                 if (touchDownTime == 0) {
    73.                     touchDownTime = Time.time;
    74.                     touchDownPos = pos;
    75.                 }
    76.                 touchUpPos = pos;    // (update this continually during the touch)
    77.             } else if (touchDownTime > 0) {
    78.                 // Touch-up: trigger a swipe if we have moved a sufficient distance
    79.                 // since touch-down, within the last second.
    80.                 if (Time.time - touchDownTime < 1 && Vector2.Distance(touchDownPos, touchUpPos) > 0.5f) {
    81.                     float ang = Mathf.Atan2(touchUpPos.y - touchDownPos.y, touchUpPos.x - touchDownPos.x);
    82.                     Debug.Log("Swipe from " + touchDownPos + " to " + touchUpPos + " angle: " + ang);
    83.                     currBtnState[(int)VButton.SwipeLeft] = (ang > 135 || ang < -135);
    84.                     currBtnState[(int)VButton.SwipeRight] = (ang < 45 && ang > -45);
    85.                     currBtnState[(int)VButton.SwipeUp] = (ang > 45 && ang < 135);
    86.                     currBtnState[(int)VButton.SwipeDown] = (ang < -45 && ang > -135);                  
    87.                 }
    88.                 touchDownTime = 0;
    89.             }
    90.         }
    91.         currBtnState[(int)VButton.Trigger] = OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger);
    92.         currBtnState[(int)VButton.Back] = OVRInput.Get(OVRInput.Button.Back);      
    93.         #endif
    94.        
    95.     }
    96.    
    97.     public static bool Get(VButton btn) {
    98.         return _instance.currBtnState[(int)btn];
    99.     }
    100.    
    101.     public static bool GetDown(VButton btn) {
    102.         return _instance.currBtnState[(int)btn] && !_instance.prevBtnState[(int)btn];
    103.     }
    104.  
    105.     public static bool GetUp(VButton btn) {
    106.         return !_instance.currBtnState[(int)btn] && _instance.prevBtnState[(int)btn];
    107.     }
    108. }
    109.  
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Oh yes, the other thing to be aware of is the Oculus "Platform SDK". This is a plug-in to hook into Oculus's own networking features, including VoIP, match-making, etc.

    Even if you don't use most of their networking stuff (we're using Photon in our current project), if you have a networked game at all you should probably use Oculus's VoIP, or get the Oculus user name. For these you will need to delve into the Platform SDK.

    If your game doesn't do any networking, then you can probably ignore this.
     
  4. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    540
    Thank you for the help!
    I will use this method with the plugin(s) (until Unity adds native input support for the Go, like for the Rift/Vive ;) )