I would like to enable gyro controlled camera onButtonClick event but I want it to start at the camera\'s current position. Currently when the gyro gets enabled it moves th
You need to get the offset camera position in the Awake
or Start
function. In the Update
function, apply that offset value to the value from the gyro sensor.
It looks like you already know offset should be used but you are not doing that the right way. The confusing part is getting the offset which requires subtracting the current camera rotation from the gyro sensor value.
To subtract a Quaternion
multiply the Inverse
not just the Quaternion
:
Quaternion = Quaternion *
Quaternion.Inverse
To add a Quaternion
multiply the Quaternion
:
Quaternion = Quaternion * Quaternion.
This is what your code should look like:
Quaternion offset;
void Awake()
{
Input.gyro.enabled = true;
}
void Start()
{
//Subtract Quaternion
offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
}
void Update()
{
GyroModifyCamera();
}
void GyroModifyCamera()
{
//Apply offset
transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}