Help my code is broke

okay i’m trying to make a ball that can launch and when you turn around the camera follows but my camera is broke and a solution I tried is to make it so it flips when the s key is pressed but that broke it more. code in comments

this is my code

public class CameraController : MonoBehaviour
    public Transform player;                // Object the camera will follow
    public float smoothSpeed = 0.125f;      // Speed at which the camera follows the player
    public float flipDistance = 10f;        // Distance from the player to flip the camera
    public float flipSpeed = 180f;          // Speed at which the camera flips to the other side
    public float cameraDistance = 5f;       // Distance between the camera and the player
    public float cameraHeight = 2f;         // Height of the camera from the player
    public float flipCooldown = 1f;         // Cooldown duration between flips

    private Vector3 offset;                 // Offset between the camera and the player
    private bool isFlipped = false;          // Check if the camera is flipped to the other side
    private float originalCameraDistance;    // Initial distance between the camera and the player
    private float flipTimer = 0f;            // Timer to track the flip cooldown

    private void Start()
        offset = new Vector3(0f, cameraHeight, -cameraDistance);
        originalCameraDistance = cameraDistance;

    private void LateUpdate()
        // Calculate the target position
        Vector3 targetPosition = player.position + offset;

        // Smoothly move the camera towards the target position
        transform.position = Vector3.Lerp(transform.position, targetPosition, smoothSpeed * Time.deltaTime);

        // Update the flip timer
        flipTimer += Time.deltaTime;

        // Check if the camera should flip to the other side
        if (Input.GetKeyDown(KeyCode.S) && flipTimer >= flipCooldown)
            isFlipped = !isFlipped;
            flipTimer = 0f;

    private void FlipCamera()
        // Calculate the new position and rotation for flipping the camera
        Vector3 flippedOffset = new Vector3(offset.x, offset.y, -offset.z);
        Vector3 lookDirection = isFlipped ? -player.forward : player.forward;
        Quaternion flippedRotation = Quaternion.LookRotation(lookDirection, Vector3.up);

        // Invert the camera distance when flipping
        cameraDistance *= -1f;

        // Update the offset based on the flipped camera distance
        offset = isFlipped ? flippedOffset : new Vector3(0f, cameraHeight, -cameraDistance);

        // Rotate the camera smoothly to the flipped rotation
        StartCoroutine(SmoothRotate(flippedRotation, flipSpeed));

    private System.Collections.IEnumerator SmoothRotate(Quaternion targetRotation, float rotationSpeed)
        Quaternion startRotation = transform.rotation;
        float elapsedTime = 0f;

        while (elapsedTime < 1f)
            elapsedTime += Time.deltaTime * rotationSpeed;
            transform.rotation = Quaternion.Lerp(startRotation, targetRotation, elapsedTime);
            yield return null;

annotated so you know what they are supposed to do but dont