Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion

So basically like this gif looks like
https://giphy.com/gifs/l0MYrjDwyvzLmj7Ms

^ left box represents gyroscope, right box represents player camera

here is the working code with eulerAngles,

gyroRotation = Input.gyro.attitude;
gyroRotation = Vector3.SlerpUnclamped(previousGyroscopeRotation, gyroRotation, fDamping * Time.deltaTime);
deltaRotation = (previousGyroscopeRotation - gyroRotation);

finalRotation = finalRotation - deltaRotation;

if (finalRotation.x != 0) 
    finalRotation.x = Mathf.LerpUnclamped(finalRotation.x, 0, 1 * Time.deltaTime);

if (finalRotation.y != 0)
    finalRotation.y = Mathf.LerpUnclamped(finalRotation.y, 0, 1 * Time.deltaTime);

if (finalRotation.z != 0)
    finalRotation.z = Mathf.LerpUnclamped(finalRotation.z, 0, 1 * Time.deltaTime);

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(finalRotation), Time.deltaTime);

previousGyroscopeRotation = gyroRotation;

(but because of angle problems (ie: jumping from 350˙ to 10´ instead of 370)) internet suggested i use Quaternions, but have no idea how to achieve the same effect

transform.rotation = Quaternion.Slerp(transform.rotation, Input.gyro.attitude, Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, zeroPosition, Time.deltaTime);

this is the peak of my skills with Quaternions, if someone could shed some light on how to achieve this as simply as possible

To solve your Euler angle problems, you can use Mathf.LerpAngle which takes the jump from 360 to 0 into account. Also, the equivalent of (0,0,0) for rotation is Quaternion.identity, since quaternions can never be zero. I don’t think using angles is a bad idea, if you know what you’re doing. Simply changing Lerp to LerpAngle should probably fix any problems you have.

Ok So I’ve used Mathf.LerpAngle, it did fix the angle problems I’ve had,
but there were still the usual EulerAngle problems, where rotation x over ~75˙, y and z axis both flip to 180, and that again screwed the system up.

So I’ve research Quaternions, and basically few lines of code does exactly what I wanted with no problems :slight_smile:

gyroRotQuat = Input.gyro.attitude;
deltaRotQuat = (previousGyroscopeRotQuat * (Quaternion.Inverse(gyroRotQuat)));
finalRotQuat = finalRotQuat * (Quaternion.Inverse(deltaRotQuat));
transform.rotation = Quaternion.Slerp(transform.rotation, deltaRotQuat, Time.deltaTime);
previousGyroscopeRotQuat = gyroRotQuat;