using UnityEngine; using UnityEngine.UI; namespace Google.Maps.Examples.Shared { /// /// This class controls the pitch elevation of the camera rig when the examples /// are deployed on a mobile device. /// public class GyroController : MonoBehaviour { /// /// Reference to the camera rig (wraps the camera object) /// public Transform CameraRig; /// /// Text field for feedback on Gyro availability /// public Text InfoTxt; /// /// Keeps track of the euler rotation on the forward axis /// private float AngleX; /// /// Reference to the transform of the camera /// private Transform CameraTransform; /// /// Reference to the gyroscope when available /// private Gyroscope Gyro; /// /// Indicates if the gyroscope is supported on the device where the app is deployed /// private bool GyroSupported; /// /// At start, we detect the availability of the gyroscope and keep track of camera transform /// and euler rotation on the X axis. /// private void Start() { GyroSupported = SystemInfo.supportsGyroscope; if (GyroSupported) { Gyro = Input.gyro; Gyro.enabled = true; } if (CameraRig != null) { var c = CameraRig.gameObject.GetComponentInChildren(); if (c != null) { CameraTransform = c.transform; AngleX = CameraTransform.rotation.eulerAngles.x; } } if (InfoTxt != null) { InfoTxt.text = ""; } } /// /// During updates, keep track of the rotation diffs around the X axis, /// and apply the new combined angle to the camera transform. /// private void Update() { if (GyroSupported) { if (CameraRig != null) { AngleX += -Input.gyro.rotationRateUnbiased.x; AngleX = Mathf.Clamp(AngleX, -30f, 90f); CameraTransform.transform.localRotation = Quaternion.Euler( AngleX, 0f, 0f); } } else { InfoTxt.text = "Gyro Not supported !"; } } } }