Last active
November 10, 2020 18:38
-
-
Save chanibal/baf46307c4fee3c699d5 to your computer and use it in GitHub Desktop.
Unity3d relative gyroscope demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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