- Home /
Question by
Amal_Joy · Jul 01, 2019 at 05:42 PM ·
androidmobile devicesgyroscoperotation detection
How to rotate game objects using mobile gyroscope?
I have a game object in scene( let it be a cube). I need to rotate it along x,y and z axis on the exact amount on which my mobile phone is rotated around these axis. I am using gyroscope sensor as input. "RotateRate" was working fine initially, but after rotating and shaking phone a bit, the orientation of cube doesn't match as that of phone. . here is my code, .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class qwerty : MonoBehaviour
{
private bool gyroEnabled;
private Gyroscope gyro;
float RtD = 0;
void Start()
{
RtD = 57.324f;
gyroEnabled = EnableGyro();
}
private bool EnableGyro()
{
if(SystemInfo.supportsGyroscope)
{
gyro = Input.gyro;
gyro.enabled = true;
Debug.Log("gyro detected");
return true;
}
else
{
Debug.Log("gyro not detected");
return false;
}
}
private void Update()
{
gameObject.transform.Rotate(gyro.rotationRate.x * Time.deltaTime*RtD, gyro.rotationRate.y * Time.fixedDeltaTime*RtD, gyro.rotationRate.z * Time.fixedDeltaTime*RtD);
}
}
Comment