Question

IHi I've been working on a camera rotation method.

I'm trying to rotate a camera using the mouse inputs. I need the horizontal rotation to be parallel to a x,y plane(my board) and the vertical rotation to be limited to certain angle(something like 40 degree to -40).

I use quaternions to rotate the camera.

Here my code so far

float angle = (mouseX_Current - mouseX_ActionStart) * 0.25f;
Quaternion horizontalRotationQuat = Quaternion.AngleAxis(angle, Vector3.up);

angle = (mouseY_Current - mouseY_ActionStart) * 0.25f;
Quaternion verticalRotationQuat = Quaternion.AngleAxis(angle, Vector3.right);

Camera.main.transform.rotation *= (horizontalRotationQuat * verticalRotationQuat);

My problem is that by adding those mouse related rotations to my current camera orientation quaternion the camera is no more parallel to the plane(x,y) which it s looking at.

I' ve been trying to create a correction quaternion to add to the camera quaternion after adding those mouse related rotations but I can't seem to find the right one.

Quaternion currentOrientationQuat = Camera.main.transform.rotation;

Quaternion corretionQuat = new Quaternion(0.0f, 0.0f, - currentOrientationQuat.z, currentOrientationQuat.w);

Camera.main.transform.rotation *= corretionQuat;

If someone could help me out on this I would be very grateful. Thanks.

Sorry for the English, not my primary language.

Was it helpful?

Solution

Solved this using another method

float angle = (mouseX_Current - mouseX_ActionStart) * 0.25f;
Camera.main.transform.Rotate(0.0f, 0.0f, angle, Space.World);

angle = (mouseY_Current - mouseY_ActionStart) * 0.25f;
Camera.main.transform.Rotate(angle, 0.0f, 0.0f, Space.Self);

Feeling dumb after seeing how easy it was to do it. Did'nt even needed the quaternions for this one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top