Skip to content

Instantly share code, notes, and snippets.

@peroon
Created June 16, 2016 06:20
Show Gist options
  • Save peroon/33169b18ecaef2e172c4bb5766321ea9 to your computer and use it in GitHub Desktop.
Save peroon/33169b18ecaef2e172c4bb5766321ea9 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class GyroScene : MonoBehaviour {
public Transform target;
private Quaternion initialRotation;
private Quaternion gyroInitialRotation;
private bool isInitialized = false;
void Start(){
StartCoroutine (StartCoroutine ());
}
IEnumerator StartCoroutine(){
Input.gyro.enabled = true;
yield return new WaitForSeconds (1.0f); // Gyro enable needs some time
initialRotation = target.rotation;
gyroInitialRotation = Input.gyro.attitude;
isInitialized = true;
}
void Update () {
if (isInitialized) {
Debug.Log (gyroInitialRotation);
Quaternion offsetRotation = Quaternion.Inverse (gyroInitialRotation) * Input.gyro.attitude;
target.rotation = initialRotation * offsetRotation;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment