简体   繁体   English

在Unity3d中使用Android陀螺仪,如何将初始摄像机旋转设置为初始移动设备旋转?

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

I want to use the Android gyroscope to perform head tracking on the standard First Person Controller of Unity3d. 我想使用Android陀螺仪在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);
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM