- Home /
Question by
forferdeilig · Jul 13, 2018 at 01:02 AM ·
quaternionbug-perhapsmobile devicesgyroscopequaternions
using difference in 2 quaternions to cancel out starting position of gyro
Hi, i'm trying to have my camera pointing the same way when the game starts, no matter which direction the user is facing when it starts. I have 3 variables to store the initial rotation of the gyro, the initial rotation of the object, and the difference between the 2.
It seems to put the camera the the correct orentation BUT:
1) it only points the camera the right way if you click play, then stop then play again without moving the phone (always wrong on 1st play always right on second)
2) the axes of the camera and gyro do not match up, i.e. tilt pans and rotate tilts etc.
I'm still a very very novice coder and even more novice at quaternions, and it's taken me about 2 weeks to get this far so i could really use some help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gryoCam : MonoBehaviour {
Quaternion gyroStartRot;
Quaternion objectStartRot;
Quaternion diffStartRot;
// Use this for initialization
void Start () {
Input.gyro.enabled = true;
gyroStartRot = GyroToUnity(Input.gyro.attitude);
Debug.Log (gyroStartRot.eulerAngles);
transform.localRotation = Quaternion.Euler (0, -90, 0); //Set initial orientation
objectStartRot = transform.localRotation;
Debug.Log (objectStartRot.eulerAngles);
//get difference between gyro start position and where it should be pointing(objectStartRot)
diffStartRot = Quaternion.Inverse(objectStartRot)*gyroStartRot;
Debug.Log (diffStartRot.eulerAngles);
}
// Update is called once per frame
void Update () {
//transform.localRotation = gyroStartRot*Quaternion.Inverse(diffStartRot); // THIS WORKS
transform.localRotation = GyroToUnity(Input.gyro.attitude)*Quaternion.Inverse(diffStartRot); // THIS DOESNT WORK
}
// The Gyroscope is right-handed. Unity is left handed.
// Make the necessary change to the camera.
void GyroModifyCamera()
{
}
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
}
Comment
Your answer
