Search Unity

Oculus Go Controller

Discussion in 'VR' started by ClaudiaDu98, Sep 15, 2018.

  1. ClaudiaDu98

    ClaudiaDu98

    Joined:
    Sep 5, 2018
    Posts:
    6
    I just started using Unity 6 weeks ago. Can someone tell me the start to finish method for putting the Oculus go pointer/cursor into a scene, and having the controller prefab tracked (so if the user is waving it or rotating it shows in the scene play mode)? Doesn't have to have the line renderer. Please help, the tutorials on YouTube aren't that helpful
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Sure, it's pretty easy. Create the following script:

    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.     System.IO.StreamWriter recording;
    12.  
    13.     void Awake() {
    14.         #if UNITY_EDITOR
    15.         leftHanded = false;        // (whichever you want to test here)
    16.         #else
    17.         leftHanded = OVRInput.GetControllerPositionTracked(OVRInput.Controller.LTouch);
    18.         #endif
    19.     }
    20.  
    21.     void Update() {
    22.         OVRInput.Controller c = leftHanded ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch;
    23.         if (OVRInput.GetControllerPositionTracked(c)) {
    24.             controller.localRotation = OVRInput.GetLocalControllerRotation(c);
    25.             controller.localPosition = OVRInput.GetLocalControllerPosition(c);
    26.         }
    27.     }  
    28. }
    29.  
    Attach this script to some object in the scene.

    Now create whatever you want to represent the controller visually (I typically start in a new project with just a scaled cube, with maybe another cube sticking out of one face like a wand). Drag that into the controller slot on object where you attached the above script.

    That's it. Build & run, and you should see your object moving around as you move the physical controller.

    Note that we're only changing the local position and rotation with this script. And as it happens, the XR system only changes the local rotation of the camera. Probably you'll want to put the visual controller object, and the main camera, under some other game object that represents the player. Then you can move/rotate this player object, and both the camera and the controller will go along with it.
     
  3. ClaudiaDu98

    ClaudiaDu98

    Joined:
    Sep 5, 2018
    Posts:
    6
    Thanks so much JoeStrout, it's actually working!!
     
    JoeStrout likes this.
  4. coffiarts

    coffiarts

    Joined:
    Jan 7, 2013
    Posts:
    33
    Thanks, JoeStrout, for providing this solution!
    Out of curiosity: Is there a particular reason, why you're reinitializing the c variable every tick in Update().
    Wouldn't it be sufficient to do this once in Awake()?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, the reason is, players can switch the handedness of their controller while your app is running. And if you fail to detect that and deal with it properly, then you fail the Technical Review, and Beatron 2000 doesn't come out till a week after Thanksgiving!
     
  6. coffiarts

    coffiarts

    Joined:
    Jan 7, 2013
    Posts:
    33
    Ok, I see! Thanks.
    So I guess it's not THAT relevant for a mere private project when I am deploying only to my own device in developer mode? :)
     
  7. MarkVincent

    MarkVincent

    Joined:
    Nov 18, 2014
    Posts:
    26
    :)
     
  8. dakomz

    dakomz

    Joined:
    Nov 12, 2019
    Posts:
    40
    Is there any way to see this in the editor?

    More detail:

    I added the OVRCameraRig prefab - so holding `ctrl` and moving the mouse lets me control the camera

    I also created an object and added the above script to it. I would now like to be able to control it in the editor so that I can test actions like "moving the wand around and firing projectiles"

    Thanks!