在Unity3d中使用Android陀螺仪,如何将摄像机的初始旋转设置为移动设备的初始旋转? [英] Using the Android gyroscope in Unity3d, how can I set the initial camera rotation to the initial mobile device rotation?

查看:244
本文介绍了在Unity3d中使用Android陀螺仪,如何将摄像机的初始旋转设置为移动设备的初始旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Android陀螺仪在Unity3d的标准第一人称"控制器上执行头部跟踪.我创建了一个简短的脚本,可以同时旋转第一人称控制器"的父节点和相机子节点.脚本已附加到相机上.

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

推荐答案

只需将初始方向保存在两个变量中,您的代码将变为:

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

这篇关于在Unity3d中使用Android陀螺仪,如何将摄像机的初始旋转设置为移动设备的初始旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆