C# 使用Unity3d中的Android陀螺仪,如何将初始相机旋转设置为初始移动设备旋转?

C# 使用Unity3d中的Android陀螺仪,如何将初始相机旋转设置为初始移动设备旋转?,c#,android,unity3d,gyroscope,C#,Android,Unity3d,Gyroscope,我想使用Android陀螺仪在Unity3d的标准第一人称控制器上执行头部跟踪。我创建了一个短脚本,用于旋转第一人称控制器的父节点和摄影机子节点。脚本已附加到摄影机 这个脚本工作得很好,它根据我的移动设备的移动来旋转第一人称视图。但是,只有当我启动应用程序时,将手机保持在向前看的位置时,它才会起作用。如果我的手机平放在桌子上,启动应用程序,相机和陀螺仪的旋转都会关闭 我希望我的脚本尊重初始设备旋转。当我启动我的应用程序,我的设备屏幕向上时,相机最初也应该向上看。如何修改脚本以将相机旋转设置为初始

我想使用Android陀螺仪在Unity3d的标准第一人称控制器上执行头部跟踪。我创建了一个短脚本,用于旋转第一人称控制器的父节点和摄影机子节点。脚本已附加到摄影机

这个脚本工作得很好,它根据我的移动设备的移动来旋转第一人称视图。但是,只有当我启动应用程序时,将手机保持在向前看的位置时,它才会起作用。如果我的手机平放在桌子上,启动应用程序,相机和陀螺仪的旋转都会关闭

我希望我的脚本尊重初始设备旋转。当我启动我的应用程序,我的设备屏幕向上时,相机最初也应该向上看。如何修改脚本以将相机旋转设置为初始移动设备旋转

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

只需将初始方向保存在两个变量中,代码就变成:

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

@图伊加:它能回答你的问题吗?有没有办法锁定Z轴。以圆周运动旋转手机时,Z轴会松动,即使将其设置为0,它仍会更改旋转值。