Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Gyroscope : MonoBehaviour {
- private bool gyroEnabled;
- private UnityEngine.Gyroscope gyro;
- private Compass compa;
- private GameObject cameraContainer;
- private Quaternion rot;
- private void Start(){
- cameraContainer = new GameObject("Camera Container");
- cameraContainer.transform.position = transform.position;
- transform.SetParent(cameraContainer.transform);
- gyroEnabled = EnableGyro();
- Input.compass.enabled = true;
- }
- private bool EnableGyro(){
- if(SystemInfo.supportsGyroscope){
- gyro = Input.gyro;
- gyro.enabled = true;
- cameraContainer.transform.rotation = Quaternion.Euler(90f, 90f, 0f);
- rot = new Quaternion(0,0,1,0);
- return true;
- }
- return false;
- }
- private void Update(){
- if(gyroEnabled){
- transform.localRotation = gyro.attitude * rot;
- }
- /* Let's go set up Input for Android without Gyroscope Technologie ! */
- else if(!gyroEnabled){
- ActiveCompass();
- }
- }
- private void ActiveCompass(){
- transform.rotation = Quaternion.Slerp(transform.rotation,
- Quaternion.Euler(0,Input.compass.magneticHeading, 0), Time.deltaTime*2);
- }
- private void OnGUI(){
- GUI.TextArea(new Rect(100,0,100,100), Input.compass.magneticHeading.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment