Using the Android gyroscope in Unity3d, how can I set the initial camera rotation to the initial mobile device rotation?

19,490

Just save the initial orientation in two variables, your code become :

using UnityEngine;
using System.Collections;

// Activate head tracking using the gyroscope
public class HeadTracking : MonoBehaviour {
    public GameObject player; // First Person Controller parent node
    public GameObject head; // First Person Controller camera

    // The initials orientation
    private int initialOrientationX;
    private int initialOrientationY;
    private int initialOrientationZ;

    // Use this for initialization
    void Start () {
        // Activate the gyroscope
        Input.gyro.enabled = true;

        // Save the firsts values
        initialOrientationX = Input.gyro.rotationRateUnbiased.x;
        initialOrientationY = Input.gyro.rotationRateUnbiased.y;
        initialOrientationZ = -Input.gyro.rotationRateUnbiased.z;
    }

    // Update is called once per frame
    void Update () {
        // Rotate the player and head using the gyroscope rotation rate
        player.transform.Rotate (0, initialOrientationY -Input.gyro.rotationRateUnbiased.y, 0);
        head.transform.Rotate (initialOrientationX -Input.gyro.rotationRateUnbiased.x, 0, initialOrientationZ + Input.gyro.rotationRateUnbiased.z);
    }
}
Share:
19,490
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to use the Android gyroscope to perform head tracking on the standard First Person Controller of Unity3d. I created a short script that rotates both the parent node and the camera child node of the First Person Controller. The script is attached to the camera.

    This script works very well, it rotates the first-person view based on the movements of my mobile device. However, it only works when I hold my phone in a forward looking position when I start my app. If my phone lies flat on the table and I start my app, both the camera and gyroscope rotations are off.

    I would like my script to respect the initial device rotation. When I start my app and my device has the screen up, the camera should initially also look up. How can I modify my script to set the camera rotation to the initial mobile device rotation?

    using UnityEngine;
    using System.Collections;
    
    // Activate head tracking using the gyroscope
    public class HeadTracking : MonoBehaviour {
        public GameObject player; // First Person Controller parent node
        public GameObject head; // First Person Controller camera
    
        // Use this for initialization
        void Start () {
            // Activate the gyroscope
            Input.gyro.enabled = true;
        }
    
        // Update is called once per frame
        void Update () {
            // Rotate the player and head using the gyroscope rotation rate
            player.transform.Rotate (0, -Input.gyro.rotationRateUnbiased.y, 0);
            head.transform.Rotate (-Input.gyro.rotationRateUnbiased.x, 0, Input.gyro.rotationRateUnbiased.z);
        }
    }
    
  • Ludovic Feltz
    Ludovic Feltz over 9 years
    @tuyga does it answer your question?
  • Warface
    Warface about 8 years
    Is there a way to lock the Z axis. When turning the phone in circle motion, the Z axis is going loose and even with setting it to 0, it's still changing rotation value.