How to save Camera Rotation

I have a Script where if I press down my mouse my Camera will move either to the left or to the right however much the mouse moves in that x direction.
But every time I take my finger off my mouse and press again it resets to 0 because of how I programmed it. I don’t know how I could save the Rotation of the Camera so it doesn’t always start at 0 when I click my mouse. Can anyone help me? This is my Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraScript : MonoBehaviour
{
    Vector3 pos; 
    public float moveSpeed = 10f;
    public float initialMouseX;
    public float mouseXMove;
    public float rotationSpeed = 10f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButton("Fire1"))
        {
            mouseXMove = initialMouseX - Input.mousePosition.x / 4;
            Debug.Log(mouseXMove);
            Quaternion target = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 
            mouseXMove, transform.rotation.z);
            transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 
            moveSpeed);
        }

        if (Input.GetButtonDown("Fire1"))
        {
            initialMouseX = Input.mousePosition.x;
        }
    }
}

Hi!

You just need to include some starting rotation variable which remembers the rotation before you clicked and includes it when calculating the new position:

if (Input.GetButtonDown("Fire1"))
{
    initialMouseX = Input.mousePosition.x;
}

if (Input.GetButton("Fire1"))
{
    movementSinceMouseDown = (initialMouseX - Input.mousePosition.x) * sensitivity;

    Vector3 target = transform.eulerAngles;
    target.y = startingRotation + movementSinceMouseDown;

    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(target), Time.deltaTime * moveSpeed);
}

if (Input.GetButtonUp("Fire1"))
{
    startingRotation += movementSinceMouseDown;
}

I have renamed and rearranged some variable in an effort to make things more simple.

Be aware that you were using transform.rotation.x in line 25 of your original script. You probably don’t want to be using it as it is one of the 4 values of a quaternion which range from -1 to 1 and don’t clearly relate to the values you should have been using, namely transform.eulerAngles.x, which is the familiar 3 numbers from 0 to 360 that we all know and love.

Also there were some weird effects in line 23 of your original script, because you weren’t using parentheses properly.

The vars:

public float moveSpeed = 10f;
public float sensitivity = 0.8f;

public float initialMouseX;
public float startingRotation;
public float movementSinceMouseDown;

Hope this helps!