- Home /
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!
Answer by MedievalPete · May 21, 2015 at 01:24 PM
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);
}
Thank you! I was thinking about using the gyroscope in my new game, and it all it took is your one line!!
I've used this code and works excellently except that the camera moves too quickly in-game, is there any way to slow down how fast this moves the camera?
Just do this. It works for me
Thank you @$$anonymous$$edievalPete for the answer!
void Update ()
{
player.transform.Rotate (0, -Input.gyro.rotationRateUnbiased.y/2, 0);
}
Thanks, it works like a charm (y) I have modify the code according to my requirement float sens = 50.0f; // to control the speed of camera rotaion void Update() {
transform.Rotate(0, -Input.gyro.rotationRateUnbiased.y sens Time.deltaTime, 0); }
Answer by fallengamer · Nov 21, 2016 at 03:25 PM
I want to know how I can integrate for the both axis of gyro ?
Hello
By using the same logic you can add
void Update () { player.transform.Rotate (-input.gyro.rotationRateUnbiased.x, -Input.gyro.rotationRateUnbiased.y, 0); }
This works really well, but it moves the camera way too fast and is very jittery, is there a way to slow down the input?
If you want a rotation whit more flow take a look on Quaternion.Slerpt and if you want to apply less rotation just use a variable to divide the parameter of your input.
Answer by makaka-org · May 17, 2019 at 08:44 AM
Hi, Guys! I used technique of camera rotating with Gyro in my AR Cameras:
1. AR Camera GYRO;
2. AR + VR: Mixed Reality.
Asset Documentation.
Answer by bionicartsmobile · Jun 10, 2020 at 04:00 AM
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)