Help Smoothing Rotation/Smoothing Mouse Input

I am rotating my player character using this line of code

rb.MoveRotation(Quaternion.LookRotation(transform.forward + (Vector3.up * Input.GetAxis("Mouse Y") * ySens + Input.GetAxis("Mouse X") * transform.right) * xSens * 0.05f));

and in third person it seems to work fine but if I move the camera into a first person perspective the movement feels jumpy and a little jittery.

Any suggestions on how to smooth the movement out or get rid of any micro movements from the mouse.

I like using Lerp() as a low-pass filter.

Lerping to smooth things out:

https://discussions.unity.com/t/802366/2

https://discussions.unity.com/t/710168/2

In the case of input smoothing (implementing your own input filtering):

https://discussions.unity.com/t/807121/2

https://discussions.unity.com/t/818601/4

Also, yowee, I have no idea how to reason about code like this:

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.

The longer your lines of code are, the harder they will be for you to understand them.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

I have it in a single line because I am combining both axes of mouse input into a single quaternion that I can use rigidbody.moverotation on. I could have the quaternion.lookrotation on a different line but I don’t really see the value of that because it would just be

Quaternion q = Quaternion.LookRotation(transform.forward + (Vector3.up * Input.GetAxis("Mouse Y") * ySens + Input.GetAxis("Mouse X") * transform.right) * xSens * 0.05f);
rb.MoveRotation(q);

Oh I recognize this way of doing a minimalist mouse look!. :slight_smile:

Because you’re adding a sensitivity for each axis the 0.05f at the end your line is probably redundant and may even be giving you issues because it’s reducing your input to a very small value.
Try this:

rb.MoveRotation(Quaternion.LookRotation(transform.forward + Vector3.up * Input.GetAxis("Mouse Y") * ySens + transform.right * Input.GetAxis("Mouse X") * xSens));

A slightly simpler method is this:

transform.forward+=Vector3.up*Input.GetAxis("Mouse Y")*ySens + transform.right*Input.GetAxis("Mouse X")*xSens;

The rb.MoveRotation was used because it allows for some interaction with rigidbodies when rotating. Although it’s not perfect because the method is meant to be used in FixedUpdate on a kinematic rigidbody. So don’t use MoveRotation on a regular rigidbody in FixedUpdate or you could get lots of jitter. Use it in Update instead…

Unity is incredibly quirky…