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

tuyga picture tuyga · Nov 13, 2014 · Viewed 19.3k times · Source

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);
    }
}

Answer

Ludovic Feltz picture Ludovic Feltz · Nov 13, 2014

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);
    }
}