Advertisement

Change axes of a gyro

Started by February 11, 2021 08:43 AM
0 comments, last by Jannik 3 years, 8 months ago

I want to change the axes of the gyro control so that the mobile phone does not have to be held upright, but lies flat on a table, but i don't know which parameters i need to change. Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Gyrocontrol : MonoBehaviour
{
    private bool gyroEnabled;
    private Gyroscope gyro;
 
    private GameObject Cube;
    private Quaternion rot;
 
    private void Start()
    {
        Cube = new GameObject("Cube");
        Cube.transform.position = transform.position;
        transform.SetParent(Cube.transform);
 
        gyroEnabled = EnableGyro();
    }
    private bool EnableGyro()
    {
        if (SystemInfo.supportsGyroscope)
        {
            gyro = Input.gyro;
            gyro.enabled = true;
         
            Cube.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;
        }
    }
}
 

This topic is closed to new replies.

Advertisement