Skip to content

Instantly share code, notes, and snippets.

@Adjuvant
Created May 3, 2018 15:11
Show Gist options
  • Select an option

  • Save Adjuvant/71a6c14f9d9020fef4cf4cb4b5f528b8 to your computer and use it in GitHub Desktop.

Select an option

Save Adjuvant/71a6c14f9d9020fef4cf4cb4b5f528b8 to your computer and use it in GitHub Desktop.
Simple gyro phone rotation in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GyroInput : MonoBehaviour {
Gyroscope m_Gyro;
public float speed = 0.1f;
float hRotation = 0.0f; //Horizontal angle
float vRotation = 0.0f; //Vertical rotation angle of the camera
void Start()
{
//Set up and enable the gyroscope (check your device has one)
m_Gyro = Input.gyro;
m_Gyro.enabled = true;
}
private void Update()
{
hRotation -= m_Gyro.rotationRate.y * speed;
transform.Rotate(Vector3.up, hRotation);
}
//This is a legacy function, check out the UI section for other ways to create your UI
void OnGUI()
{
//Output the rotation rate, attitude and the enabled state of the gyroscope as a Label
GUI.Label(new Rect(500, 300, 200, 40), "Gyro rotation rate " + m_Gyro.rotationRate);
GUI.Label(new Rect(500, 350, 200, 40), "Gyro attitude" + m_Gyro.attitude);
GUI.Label(new Rect(500, 400, 200, 40), "Gyro enabled : " + m_Gyro.enabled);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment