Hello guys,
I am creating a VR app that allows the user to look around in 360 spherical. but im having a problem with the y-axis.
The x-axis works fine, I can rotate the phone around, and the image moves along the x-axis properly.
The y-axis is the problem. When I make the phone look up or down, the x-axis seems to be moving left or right slightly, depending on whether i look up or down. I don’t want that. I just want the y-axis to move up and down, w/o the x-axis moving along as well, unless i move it in a slope.
here is the code that i found in the unity forum that i am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gyroCamera : MonoBehaviour
{
private float initialYAngle = 0f;
private float appliedGyroYAngle = 0f;
private float calibrationYAngle = 0f;
void Start()
{
Application.targetFrameRate = 60;
initialYAngle = transform.eulerAngles.y;
Input.gyro.enabled = true;
}
void Update()
{
ApplyGyroRotation();
ApplyCalibration();
}
void OnGUI()
{
if (GUILayout.Button("Calibrate", GUILayout.Width(300), GUILayout.Height(100)))
{
CalibrateYAngle();
}
}
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.
}
}