Advertisement
sphinx2001

GyroControl

Feb 26th, 2020
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class GyroControl : MonoBehaviour
  6. {
  7.     private bool gyroEnabled;
  8.     private Gyroscope gyro;
  9.  
  10.     private GameObject cameraContainer;
  11.     private Quaternion rot;
  12.  
  13.     private void Start()
  14.     {
  15.         cameraContainer = new GameObject("Camera Container");
  16.         cameraContainer.transform.position = transform.position;
  17.         transform.SetParent(cameraContainer.transform);
  18.  
  19.         gyroEnabled = EnableGyro();
  20.     }
  21.  
  22.     private bool EnableGyro()
  23.     {
  24.         if(SystemInfo.supportsGyroscope)
  25.         {
  26.             gyro = Input.gyro;
  27.             gyro.enabled = true;
  28.  
  29.             cameraContainer.transform.rotation = Quaternion.Euler(90f, 90f, 0f);
  30.             rot = new Quaternion(0, 0, 1, 0);
  31.  
  32.             return true;
  33.         }
  34.  
  35.         return false;
  36.     }
  37.  
  38.     private void Update()
  39.     {
  40.         if(gyroEnabled)
  41.         {
  42.             transform.localRotation = gyro.attitude * rot;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement