Nekrosincoding

Gyroscope

Jun 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Gyroscope : MonoBehaviour {
  6.  
  7.     private bool gyroEnabled;
  8.     private UnityEngine.Gyroscope gyro;
  9.     private Compass compa;
  10.     private GameObject cameraContainer;
  11.     private Quaternion rot;
  12.  
  13.     private void Start(){
  14.         cameraContainer = new GameObject("Camera Container");
  15.         cameraContainer.transform.position = transform.position;
  16.         transform.SetParent(cameraContainer.transform);
  17.  
  18.         gyroEnabled = EnableGyro();
  19.         Input.compass.enabled = true;
  20.     }
  21.  
  22.     private bool EnableGyro(){
  23.         if(SystemInfo.supportsGyroscope){
  24.             gyro = Input.gyro;
  25.             gyro.enabled = true;
  26.  
  27.             cameraContainer.transform.rotation = Quaternion.Euler(90f, 90f, 0f);
  28.             rot = new Quaternion(0,0,1,0);
  29.  
  30.             return true;
  31.         }
  32.         return false;
  33.     }
  34.  
  35.     private void Update(){
  36.         if(gyroEnabled){
  37.             transform.localRotation = gyro.attitude * rot;
  38.         }
  39.         /* Let's go set up Input for Android without Gyroscope Technologie ! */
  40.         else if(!gyroEnabled){
  41.             ActiveCompass();
  42.         }
  43.     }
  44.  
  45.     private void ActiveCompass(){
  46.         transform.rotation = Quaternion.Slerp(transform.rotation,
  47.         Quaternion.Euler(0,Input.compass.magneticHeading, 0), Time.deltaTime*2);
  48.     }
  49.  
  50.     private void OnGUI(){
  51.         GUI.TextArea(new Rect(100,0,100,100), Input.compass.magneticHeading.ToString());
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment