Hello everyone!
I am trying to rotate a camera using a device’s gyroscope.
While applying rotation on x and y works fine independently, trying to do both results in rotation on z, too.
Here are some code examples on what works and what doesn’t:
(gyroRot in the examples is Input.gyro.rotationRate)
// this only rotates on x
// on y and z, it rotates 0.0f
cameraObj_transform.Rotate(
- gyroRot.x,
0.0f,
0.0f
);
// this only rotates on y
// on x and z, it rotates 0.0f
cameraObj_transform.Rotate(
0.0f,
- gyroRot.y,
0.0f
);
// this rotates on x and y, but also on z
// even though z is set to 0.0f
cameraObj_transform.Rotate(
- gyroRot.x,
- gyroRot.y,
0.0f
);
Does anybody know what’s causing this?
Best wishes,
Shu
Store the rotations separately and rebuild the rotation every frame should solve it.
float pitch, yaw;
Update()
{
pitch += gyroRot.y;
yaw += gyroRot.x;
cameraObj_transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
1 Like
@GroZZleR
Thanks for your answer! It does work like that!
But shouldn’t it work using .Rotate() as well? Or am I misunderstanding the usage of .Rotate()?
Its because @GroZZleR implementation is going from a frame of reference that the object is at 0 rotation in all 3 axis each time. He’s just remember how far he rotated in previous frames and rotating the total amount needed from a reference of (0,0,0) rotation.
Your way you rotate around the X,Y axis in one frame. Now your rotating about the X,Y axis again… but its not the original 0,0,0 Your not rotating about the world Axis, but your own object’s new X,Y,Z rotation. SO it is completely rotating around its X,Y from its perspective… But your still looking at it from the perspective of a (0,0,0) rotation so it appears to be rotating around the Z.
1 Like
@takatok
I see! Thanks for the explanation! That’s tricky!