Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to get Oculus angle/rotation?

Discussion in 'AR/VR (XR) Discussion' started by guru20, Jul 10, 2015.

  1. guru20

    guru20

    Joined:
    Jul 30, 2013
    Posts:
    239
    Hello, I was wondering what the method would be to check/detect the viewing angle or rotation of the Oculus VR headset. Is it as simple as grabbing it from the camera object's transform properties, or is a different method required?
     
  2. guru20

    guru20

    Joined:
    Jul 30, 2013
    Posts:
    239
  3. guru20

    guru20

    Joined:
    Jul 30, 2013
    Posts:
    239
    Tried this, with no compilation errors but also nothing showing in the console at runtime:

    Code (CSharp):
    1. void Update () {
    2.         angles = InputTracking.GetLocalRotation (VRNode.CenterEye);
    3.         print ("X: " + angles.eulerAngles.x + "  Y: " + angles.eulerAngles.y + "  Z: " + angles.eulerAngles.z);
    4.     }
    (I also tried VRNode.head, but not sure if that is for head-tracking, which I am currently not using)

    When I did the above code but used transform.eulerAngles.x etc. on the Camera (to which I have attached this script), it does output angles to console...
     
  4. Kevin.Ferravanti

    Kevin.Ferravanti

    Joined:
    Aug 31, 2012
    Posts:
    18
    I had actually posted about this a couple days ago, you can find that here http://forum.unity3d.com/threads/unityvr-execution-order-and-transform.339415/

    my workaround was to use a coroutine and wait until EndOfFrame() before grabbing the rotation, if I have time I'll check out your InputTracking work around
     
  5. Kevin.Ferravanti

    Kevin.Ferravanti

    Joined:
    Aug 31, 2012
    Posts:
    18
    @guru20, I had a moment to look into this again and wrote a quick script I'd like to you try out, and a couple questions for you to answer, they're going to sound stupid but I'm being sincere.
    1. Are you using UnityVR? If you're using the Oculus SDK instead of the UnityVR then InputTracking probably won't work.
    2. Assuming you are using UnityVR and it's enabled, when you move your Rift around, does it move the Camera in the scene? (I had my Rift off at first, so my euler angles were all zero)
    3. Have you been running Unity for a long time? This issue seems to come and go randomly, sometimes just restarting fixes it - as scary as that is.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.VR;
    4. using System.Collections;
    5.  
    6. public class InputTrackerChecker : MonoBehaviour {
    7.     private    const string    logPrefix   = "InputTrackerChecker: ";
    8.  
    9.     [SerializeField] VRNode m_VRNode    = VRNode.Head;
    10.  
    11.     private void Start () {
    12.         StartCoroutine(EndOfFrameUpdate());
    13.     }
    14.  
    15.     private void Update () {
    16.         LogRotation("Update");
    17.     }
    18.  
    19.     private void LateUpdate () {
    20.         LogRotation("LateUpdate");
    21.     }
    22.  
    23.     private IEnumerator EndOfFrameUpdate () {
    24.         while(true) {
    25.             yield return new WaitForEndOfFrame();
    26.             LogRotation("EndOfFrame");
    27.         }
    28.     }
    29.  
    30.     private void LogRotation (string id) {
    31.         var quaternion  = InputTracking.GetLocalRotation(m_VRNode);
    32.         var euler       = quaternion.eulerAngles;
    33.         Debug.Log(string.Format("{0} {1}, ({2}) Quaternion {3} Euler {4}", logPrefix, id, m_VRNode, quaternion.ToString("F2"), euler.ToString("F2")));
    34.     }
    35. }
    36.  
    you can put it anywhere since it just queries the InputTracker (not the camera).
    I also added a VRNode value so you can check any node quickly, I defaulted it to the Head instead of the CenterEye.
    Hopefully you'll see logs like this:

    Code (CSharp):
    1. InputTrackerChecker:  Update, (CenterEye) Quaternion (-0.03, 0.21, 0.00, 0.98) Euler (357.11, 24.32, 359.54)
    2. InputTrackerChecker:  LateUpdate, (CenterEye) Quaternion (-0.03, 0.21, 0.00, 0.98) Euler (357.11, 24.32, 359.54)
    3. InputTrackerChecker:  EndOfFrame, (CenterEye) Quaternion (-0.03, 0.21, 0.00, 0.98) Euler (357.11, 24.32, 359.54)
     
  6. guru20

    guru20

    Joined:
    Jul 30, 2013
    Posts:
    239
    I have "using UnityEngine.VR" as an include

    So yeah, conveniently I just decided to start a VR project right as Unity announced native VR support, so I decided to try that instead of getting up and running with the Oculus SDK

    My camera works fine -- better now with p3 (in order to manually manipulate the camera for animations and "cut-scenes", I needed to place it inside of an empty container, and in p1 it would not rotate or orient properly, but it does in p3)

    I am going to try doing my game manipulations based on the camera rotations for now since I can't get the InputTracking to show (although I might give your code a try, as well), because the camera rotation does seem to correspond well -- just not sure if it's the most efficient method. [I want a force to be applied right or left according to tilt/lean of head left or right. This corresponds to Z movement on my camera, with a left tilt being 90 degrees and a right tilt being 270 degrees.]
     
  7. Spawn2x

    Spawn2x

    Joined:
    Oct 31, 2013
    Posts:
    1
    Hi Guru
    Have you find the answer to your question? Instead of trying seeing the values on console set public variables and you will be able to see those.
     
  8. nitinmaru

    nitinmaru

    Joined:
    Jul 31, 2015
    Posts:
    3
    HI
    I am using Oculus VR
    PLease let me know how to get orientation and how to print in UNity
    I want to use it like mouselook but instead of mouse look i want to use HMD Look for Walking direction

    Looking for Kind help
    Thank you