Search Unity

Get real world device rotation

Discussion in 'AR/VR (XR) Discussion' started by Arriaga-Mateo, Jan 9, 2019.

  1. Arriaga-Mateo

    Arriaga-Mateo

    Joined:
    Aug 21, 2018
    Posts:
    2
    Hi everyone,

    I would like to get the real world rotation of a device, but I don't clearly know how to achieve that. I'm trying to use the gyroscope attitude function, but it doesn't work for me.

    I only want the x and y rotation (in Euler angles) values, so if I move the head in a horizontal way, the y rotation angle is moving, and if I move the head verticaly, the x rotation angle moves. The z angle doesn't really matter, it will always be 0. But I want it to be in the real world rotation, so I mean that when I start on the app and I'm looking 90 degrees away from the north, that it is shown in the rotation values..

    Anyone got a clue how to achieve that?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    A gyroscope can only tell changes in rotation, not absolute orientation. For that you'd need to use the compass sensor, and hope the user isn't near any large pieces of metal or anything that generates a magnetic field.
     
  3. Arriaga-Mateo

    Arriaga-Mateo

    Joined:
    Aug 21, 2018
    Posts:
    2
    Thanks for your reply.

    I see that I misunderstood the gyroscope and the compass sensors, so thanks for that.
    After that, what I do in my code is taking the compass magnetic heading at start, to see which head orientation the user has, and then use this value as an offset for the rotation sensor that i must use. This sensor starts always at (0,0,0) rotation.

    But it still doesn't work..

    - I upload the code-
    Code (CSharp):
    1. private Vector3 rotationReceived;
    2. private float initialAttitudey;
    3.  
    4. void Start () {
    5.         Input.gyro.enabled = true;
    6.         Input.compass.enabled = true;
    7.         StartCoroutine(setRotation());
    8. }
    9.    
    10. void Update () {
    11.  
    12.         rotationReceived = GetComponent<UDPWlanReceiver>().rotation;
    13.         //Calculates the difference between the north and the actual movement
    14.         transform.rotation = Quaternion.Euler(rotationReceived.x, initialAttitudey + rotationReceived.y, rotationReceived.z);
    15. }
    16.  
    17. IEnumerator setRotation() {
    18.         //Gets the initial position of the magnetic field to see the desviation to the real north
    19.  
    20.         yield return new WaitForSeconds(1f);
    21.         initialAttitudey = Input.compass.magneticHeading;
    22.         transform.rotation = Quaternion.Euler(0, initialAttitudey, 0);
    23.     }