Skip to content

Instantly share code, notes, and snippets.

@chanibal
Last active November 10, 2020 18:38
Show Gist options
  • Save chanibal/baf46307c4fee3c699d5 to your computer and use it in GitHub Desktop.
Save chanibal/baf46307c4fee3c699d5 to your computer and use it in GitHub Desktop.
Unity3d relative gyroscope demo
using UnityEngine;
/// <summary>
/// Gyroscope demo. Attach to a visible object or camera.
/// </summary>
public class GyroTest:MonoBehaviour {
Quaternion origin=Quaternion.identity;
void Start() {
Input.gyro.enabled=true;
origin=Input.gyro.attitude;
}
void Update() {
// reset origin on touch or not yet set origin
if(Input.touchCount > 0 || origin == Quaternion.identity)
origin=Input.gyro.attitude;
transform.localRotation=Quaternion.Inverse(origin)*Input.gyro.attitude;
}
void OnGUI() {
GUILayout.Label(origin.eulerAngles+" <- origin");
GUILayout.Label(Input.gyro.attitude.eulerAngles+" <- gyro");
GUILayout.Label(Quaternion.Inverse(Input.gyro.attitude).eulerAngles+" <- inv gyro");
GUILayout.Label(transform.localRotation.eulerAngles+" <- localRotation");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment