Hey everybody,
I need to counter rotate the main camera’s local forward facing axis (z) relative to an android upward facing axis while laying flat on a table. This counter rotation should keep the game world locked on one fixed, real world axis, so that the player can spin their phone around and the game will remain facing them. I figured that the compass should do the trick but nothing I’ve tried (neither anything I could find online,) worked properly to get the screen’s outward axis. Does anyone have a solution?
This is my current attempt:
Real world up seems to be X
Game world up is Z
Camera angle: Top down
using UnityEngine;
public class Rotate_Gyro : MonoBehaviour
{
/*void Start()
{
//DeviceRotation.InitGyro();
//Input.gyro.enabled = true;
//Input.compass.enabled = true;
}*/
void Update()
{
Quaternion gyro = DeviceRotation.Get();
Vector3 rot = transform.rotation;
rot.z = gyro.eulerAngles.x; //is X up while the phone is flat? Z is up in THIS game world.
transform.rotation = Quaternion.Euler(rot);
}
}
using UnityEngine;
public static class DeviceRotation {
private static bool gyroInitialized = false;
public static bool HasGyroscope {
get {
return SystemInfo.supportsGyroscope;
}
}
public static Quaternion Get() {
if (!gyroInitialized) {
InitGyro();
}
return HasGyroscope
? ReadGyroscopeRotation()
: Quaternion.identity;
}
private static void InitGyro() {
if (HasGyroscope) {
Input.gyro.enabled = true; // enable the gyroscope
Input.gyro.updateInterval = 0.0167f; // set the update interval to it's highest value (60 Hz)
}
gyroInitialized = true;
}
private static Quaternion ReadGyroscopeRotation() {
return new Quaternion(0.5f, 0.5f, -0.5f, 0.5f) * Input.gyro.attitude * new Quaternion(0, 0, 1, 0);
}
}
Thank you,
Alec Maier.