Gyro Rotation Offest Calculation or Alternate Solution?

So I have a script where I use the gyroscope data of an iPhone to turn a camera. As of right now, my script matches the camera’s rotation with a Vector3 with altered axes, and for vertical and horizontal movements from the initialized position (phone is flat with screen pointing up), the script works fine, i.e. I turn the phone left and the camera turns left, I turn it right and the camera turns right. However, a problem i’m having is that if I turn the phone 90 degrees left/right, and then turn it 90 degrees up/down, the camera barrel rolls instead of moving up/down. If anyone knows the mathematical formula for fixing this issue, or just has a better solution, please let me know. Thanks.

You may find it easier if you have a parent object which handles panning and then apply tilt to the camera child object. Just make sure you zero out the other two unused rotation axes on each object.

e.g. parentRotation Vector3(0, pan, 0)

childRotation Vector3(tilt, 0, 0)