Skip to content

Instantly share code, notes, and snippets.

@blue0513
Created January 13, 2018 15:15
Show Gist options
  • Save blue0513/36443a54a852ba997ebbe274065ab647 to your computer and use it in GitHub Desktop.
Save blue0513/36443a54a852ba997ebbe274065ab647 to your computer and use it in GitHub Desktop.
GyroManager for Unity
using UnityEngine;
using System.Collections;
public class GyroManager : MonoBehaviour {
static bool gyroBool = false;
private Gyroscope gyro;
private Quaternion quatMult;
private GameObject camParent;
// WalkDetectorで参照する
public float slopeX = 0f;
public float slopeY = 0f;
void Awake() {
// find the current parent of the camera's transform
var currentParent = transform.parent;
// instantiate a new transform
camParent = new GameObject ("camParent");
// match the transform to the camera position
camParent.transform.position = transform.position;
// make the new transform the parent of the camera transform
transform.parent = camParent.transform;
gyroBool = SystemInfo.supportsGyroscope;
if (gyroBool) {
gyro = Input.gyro;
gyro.enabled = true;
camParent.transform.eulerAngles = new Vector3(90,90,0);
quatMult=new Quaternion(0,0,1,0);
}
Screen.sleepTimeout = SleepTimeout.NeverSleep;
screenSize=new Vector2(Screen.width,Screen.height);
}
void Update () {
if (gyroBool) {
transform.localRotation = gyro.attitude * quatMult;
// 姿勢情報を保持
slopeX = transform.eulerAngles.x;
slopeY = transform.eulerAngles.y;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment