Hi there,
I asked a question recently but I believe it was a little confusing and went un answered. I believe I understand the situation a little bit better now and am unsure on how to do it. The gyroscopes attitude is based on the direction of a compass I am guessing So north is north etc. What I need to do I think, is somehow make the gyroscopes attitude relative to the magnetic north (North in relation to the device). So that when I tilt in relation to it, no matter where the Person/Humans rotation is in the world my light will rotate in the right direction. Here’s a link to the previous thread and I’ve posted my code below. Thank you Gyroscope Tilting Rotation. Quaternions T_T Help. - Questions & Answers - Unity Discussions
private Gyroscope gyro;
public Quaternion rotation = Quaternion.identity;
private Quaternion initialGyroRotation;
private Quaternion initialRotation;
private Quaternion baseRotation;
void Start ()
{
gyro = Input.gyro;
gyro.enabled = true;
initialRotation = transform.rotation;
baseRotation = transform.rotation;
initialGyroRotation = Quaternion.identity;
transform.rotation = gyro.attitude;
Screen.orientation = ScreenOrientation.Portrait;
}
void Update ()
{
#if UNITY_IOS
Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
#endif
#if UNITY_ANDROID
Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
#endif
Quaternion offsetRotation = Quaternion.Inverse (initialGyroRotation) * attitudeFix;
rotation = initialRotation * offsetRotation;
transform.rotation = rotation;
}