Quaternion math + mouse inputs

Hey all, for some reason I can’t quite solve this one, although I feel like I’m finally starting to understand quaternions pretty well.

I’m controlling a turret, and I rotate the turret directly through mouse input thusly (x axis code is a copy of the below, but obviously with x’s):

float rotationYInput = Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
Quaternion yQuaternion = Quaternion.AngleAxis(rotationYInput, -Vector3.right);
this.transform.localRotation = this.transform.localRotation * yQuaternion;

Now, I want to make this feel more mechanical and have the movement ability restricted, so it’s not just based on mouse-sensitivity. Something closer to the player using controls on an actual machine. So I switched to lerping the rotation like this:

float rotationYInput = Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
Quaternion yQuaternion = Quaternion.AngleAxis(rotationYInput, -Vector3.right);
this.transform.localRotation = Quaternion.Lerp(this.transform.localRotation, yQuaternion, turnSpeed);

Close but no cigar - it’s using the Y-Input as the target, so it always goes back to forward after I fight to move it away from forward :). I know what’s going wrong, but I’m having a hard time figuring out the fix with quats. I need to take the input quat (yQuaternion) and add that to the localRotation, with is what I thought I was doing in the Lerp. Ideas?

Make rotationYInput a property of your script (e.g. outside the immediate function), and ADD GetAxis to it each frame. You’ll probably also want to clamp it between some sensible limit values.

Oh! Duh! Do it before the quat math! Haha, of course. For curiosity’s sake, is there any way to do it how I was trying to? For my character controller, I remember needing to remove the use of a stored, additive, property when I was adding additional functionality. Unsure if I’ll run into that again here.

You could set yQuaternion = transform.rotation * Quaternion.AngleAxis?

I almost always recommend against using transform.rotation as anything except output, though. There’s too many ways things can go wrong.