在移动设备中与陀螺仪接触时的旋转以奇怪的方式表现

时间:2018-06-06 10:20:59

标签: unity3d rotation touch gyroscope

我一直在我的项目中使用gvr sdk在通过相机播放视频时获得360度视图。但是当我在触摸时旋转相机时,旋转本身表现得很奇怪,即在横向模式下,y轴的旋转工作正常但是当陀螺仪向右或向左移动时,x轴旋转是表现为z轴旋转。请帮忙。下面是使用设备上的触摸输入在x和y轴上进行简单旋转的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RCPlayer : MonoBehaviour
{
    public static RCPlayer Instance;
    //public GameObject Head;
    //public GameObject Camera;

    Vector3 FirstPoint;
    Vector3 SecondPoint;
    float xAngle;
    float yAngle;
    float xAngleTemp;
    float yAngleTemp;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        xAngle = 0;
        yAngle = 0;
        transform.rotation = Quaternion.Euler(yAngle, xAngle, 0);
    }

    void Update()
    {      
        if (Input.touchCount > 0)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                FirstPoint = Input.GetTouch(0).position;
                xAngleTemp = xAngle;
                yAngleTemp = yAngle;
            }

            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                SecondPoint = Input.GetTouch(0).position;

                if (FirstPoint - SecondPoint == Vector3.zero)
                {
                    return;
                }
                else
                {
                    xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
                    yAngle = yAngleTemp - (SecondPoint.y - FirstPoint.y) * 180 / Screen.height;
                    transform.rotation = Quaternion.Euler(-yAngle * 0.5f, -xAngle * 0.5f, 0.0f);
                }
            }
        }
    }   
}

0 个答案:

没有答案