Camera clamp causes jitter when reaching max rotation.

Hey guys, I was able to clamp my camera rotation for a more realistic FPS effect, although when my player hits the max angle, they are pushed back.

Does anyone have any insight on how to keep the player facing up/down without an awkward push back?

    private void RotateCamera()
    {
        Vector2 inputValues = new Vector2 (Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
 
        inputValues = Vector2.Scale(inputValues, new Vector2(lookSensitivity * smoothing, lookSensitivity * smoothing));
 
        smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputValues.x, 1f / smoothing);

        smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputValues.y, 1f / smoothing);

        currentLookingPos += smoothedVelocity;

        transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right);

        player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.transform.up);
     
        currentLookingAt.y = Mathf.Clamp(currentLookingAt.y, -80f, 80f);
    }

Why are you clamping currentLookingAt after you set the rotation of the player? Should you not clamp this before?

1 Like

Not sure, I am new to coding. What could be done to improve that?

Here’s an FPS mouse-to-look-around script, from a Brackeys tutorial lol:
You need to use quaternions to not jitter i guess

public float MouseSensitivity = 500f;

    public Transform PlayerBody;

    float Xrotation = 0f;

    //Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }
   
    // Update is called once per frame
    public void Update()
    {
        float MouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
        float MouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;

        Xrotation -= MouseY;
        Xrotation = Mathf.Clamp(Xrotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(Xrotation, 0f, 0f);

        PlayerBody.Rotate(Vector3.up * MouseX);
    }