- Home /
Help with Quaternion.AngleAxis
So I'm trying to get my Camera to rotate around a single game object when I hold down the Middle Mouse Button.
My script works fine when rotating the camera around Vector3.up, but when rotating around Vector3.forward the camera moves up and down even though I am only moving the mouse down.
The problem I can see clearly, as the camera rotates, its "forward" rotation axis changes (this doesn't happen when rotating around "up" axis).
The solution? No idea, thanks in advance for your help! :)
Here's the code:
private void Update()
{
if(Input.GetMouseButton(2))
{
camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotateSpeed, Vector3.up);
camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * rotateSpeed, Vector3.forward);
camera_offset = camTurnAngle * camera_offset;
}
transform.position = targetShip.transform.position - camera_offset;
transform.LookAt(targetShip.transform);
}
Ok not sure what you’re trying to do with this code. The camTurnAngle is a quaternion rotation and the second time you call AngleAxis, it will always overwrite the first call you make on the line before, so this doesn’t make any sense and I don’t understand how it could be working on the y axis either. I’m trying to wrap my head around camTurnAngle * camera_offset.. What is camera_offset?? A float?? A vector?? Because you are multiplying the quaternion by that and then subtracting the product from the target position which is also a strange calculation. Please explain, what is camera_offset and what do you expect camTurnAngle to be?? As right now it is a quaternion.. cheers
Answer by pmerilainen · Jun 14, 2019 at 09:39 AM
If you'd check your code you can clearly see you overwrite the camTurnAngle value with the second Quaternion.AngleAxis. You should multiply quaternions if you want them to 'stack'. But bear in mind that quat multiplication is not commutative eg q1 x q2 does not equal q2 x q1