- Home /
How can I make the camera rotate with gyroscope appropriately?
I want the camera to rotate 360º degrees using the gyroscope. I have this code:
void Awake() {
Input.gyro.enabled = true;
}
void Update() {
transform.rotation = Input.gyro.attitude;
transform.Rotate(0f, 0f, 180f, Space.Self);
transform.Rotate(90f, 180f, 0f, Space.World);
}
Which works fine if I don't care about the camera having a starting rotation. But I actually want the camera to start looking with the rotation associated with the camera preview. I want it to start looking at something, and then start gyroscoping from there. So I changed the code a bit to this:
void Awake() {
Input.gyro.enabled = true;
}
void Start() {
offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
}
void Update() {
transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}
private static Quaternion GyroToUnity(Quaternion q) {
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
Now it starts looking at the right place, but when I start moving the phone around the camera rotation gets funky, it starts turning upside down. I don't know what to do, I searched everywhere. I tried this and this. Thank you for your time.
Answer by massfar2017 · Mar 01, 2019 at 11:35 AM
BUMP BUMP
Hello, i once used indoor atlas that uses this code for solving that issue: (all credit to them)
Quaternion rot = Quaternion.Inverse(new Quaternion(orientation.x, orientation.y, -orientation.z, orientation.w));
Camera.main.transform.rotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, 0.0f)) * rot;
orientation is the quaternion retrieve from the gyroscope. the previous link is a link to a demo project provided by Indoor Atlas that you can try since maybe their localization implementation might work even better for you :)
Hey sorry I am a noob, in order to use the script attached on the github, I simply need to Add Component > New Script to my scene active camera, correct?
Your answer
![](https://koobas.hobune.stream/wayback/20220612192244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Starting position gyroscope camera unity 0 Answers
multiplying gyro output 0 Answers
Rotating a camera using the gyroscope 4 Answers
Shaking screen using the gyroscope attitude 1 Answer
Calibration Script 0 Answers