I have been working on a simple Gyroscope Sensor that rotates the camera around.
This code works perfectly when I plug in Unity Remote with my Samsung Galaxy S22 (Android Version 13).
However, when I build and export the game as APK onto my phone, the Gyro Sensor simply displays (0, 0, 0) and does not rotate the camera at all
P.S. the delay of 1 second fixes a bug when the gyro sometimes does not activate.
I am suspecting it may be a problem with the Project Settings, any help would be appreciated, thanks.
public class GyroController : MonoBehaviour
{
public Transform cam;
private bool gyroEnabled;
public UnityEngine.Gyroscope gyro;
private Quaternion rotation;
private void Start()
{
StartCoroutine(DelayedStart());
}
private IEnumerator DelayedStart()
{
yield return new WaitForSeconds(1f);
gyroEnabled = EnableGyro();
}
private bool EnableGyro()
{
if (SystemInfo.supportsGyroscope)
{
Input.gyro.enabled = true;
gyro = Input.gyro;
transform.rotation = Quaternion.Euler(90f, 90f, 0f);
rotation = new Quaternion(0, 0, 1, 0);
return true;
}
return false;
}
public Text gyroscopeAttitiudeDisplay;
private void Update()
{
if (gyroEnabled)
{
cam.localRotation = gyro.attitude * rotation;
Debug.Log(gyro.attitude);
gyroscopeAttitiudeDisplay.text = $"Gyroscope: x:{gyro.attitude.x} STATE:{gyro.enabled} y:{gyro.attitude.y} z:{gyro.attitude.z}";
gyroscopeAttitiudeDisplay.text += $"
ACCEL: x:{Input.acceleration.x} y:{Input.acceleration.y} z:{Input.acceleration.z}";
}
}
}