起始位置陀螺仪相机统一

Start position gyroscope camera unity

我的 unity 场景中有一个球体内部的 360 视频,我正在使用陀螺仪脚本以便用户可以旋转并观看电影,问题是电影中有一个人,我想要相机从人的位置开始,而不是手机的位置,但我尝试这样做的方式不起作用:

public class Gyro : MonoBehaviour
{


    void Start()
    {
        if (SystemInfo.supportsGyroscope)
        {
            Input.gyro.enabled = true;
        }


            GameObject cameraParent = new GameObject("camParent");
            cameraParent.transform.position = this.transform.position;
            this.transform.parent = cameraParent.transform;
            cameraParent.transform.Rotate(Vector3.right, 90);

    }


    void Update()
    {


            Quaternion cameraRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
            this.transform.localRotation = cameraRotation;


    }
}

解决方案

void Start()
{
        Application.targetFrameRate = 60;
        initialYAngle = transform.eulerAngles.y;
        Input.gyro.enabled = true;
    }

    void Update()
    {
        if(calibra){
            CalibrateYAngle();
            calibra = false;
        }
        ApplyGyroRotation();
        ApplyCalibration();
    }



    public void CalibrateYAngle()
    {
        calibrationYAngle = appliedGyroYAngle - initialYAngle; // Offsets the y angle in case it wasn't 0 at edit time.
    }

    void ApplyGyroRotation()
    {
        transform.rotation = Input.gyro.attitude;
        transform.Rotate(0f, 0f, 180f, Space.Self); // Swap "handedness" of quaternion from gyro.
        transform.Rotate(90f, 180f, 0f, Space.World); // Rotate to make sense as a camera pointing out the back of your device.
        appliedGyroYAngle = transform.eulerAngles.y; // Save the angle around y axis for use in calibration.
    }

    void ApplyCalibration()
    {
        transform.Rotate(0f, -calibrationYAngle, 0f, Space.World); // Rotates y angle back however much it deviated when calibrationYAngle was saved.
    }
       Quaternion cameraRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
       this.transform.localRotation = cameraRotation;

这样你只需要将你的陀螺仪位置分配给你的相机,如果你想让相机从精确的位置开始,你需要add/substract将上一帧和当前帧的差异add/substract到你当前的位置