Rotating a camera using the gyroscope

Hi all. I’ve been trying for the past few days to implement a simple(!) piece of code which would allow me to control the horizontal rotation of a camera using the gyroscope of my android device. It is a first person game, in which the player should be able to ‘look around’ 360 degrees along the horizontal axis. Imagine sitting in an office chair with the device held out in front of you, then spinning round 360 degrees. As you spin, so should the camera in the game. Here’s the closest I’ve come:

using UnityEngine;
using System.Collections;
public class GyroRotate : MonoBehaviour {
	
	void Start () 
	{
		Input.gyro.enabled = true;
	}

	void Update () 
	{
		var x = Input.gyro.rotationRateUnbiased.x;
		transform.eulerAngles = new Vector3 (x, 0, 0);
	}
}

This script is attached to an empty game object with the main camera as a child. The problem with this is the camera only rotates a little, then always snaps back to its original position. Any help or advice would be hugely appreciated - I’m quite new to all of this!

I solved it - in case anyone else was curious, this was the code I needed:

void Update () 
	{
		player.transform.Rotate (0, -Input.gyro.rotationRateUnbiased.y, 0);
	}

I want to know how I can integrate for the both axis of gyro ?

Hi, Guys! I used the technique of camera rotating with Gyro in my AR Cameras:

  1. AR Camera Lite
  2. AR + VR: Mixed Reality

I love the simplicity of this rotation, but there’s a minor issue. Can you help?

So, using the following code:

void Update()
    {
        transform.Rotate(-Input.gyro.rotationRateUnbiased.x, -Input.gyro.rotationRateUnbiased.y, Input.gyro.rotationRateUnbiased.z);
    }

The Z-Axis tends to get offset after moving my device after a few seconds.
For example: If I rotate my device counter-clockwise, then the environment tends to rotate clockwise in the Z-Axis and vice versa, if I rotate my phone clockwise then the environment offsets counter-clockwise.



Is there a way to fix this? A way to correct the unwanted Z-Rotation?
(Note: even if I set the Z-Axis to Zero (0) it still has this unwanted rotation)