I can not rotate my camera left and right with this code

It is trying to, but the rotation keeps going back to 0 immediately. I am able to look up and down but not left or right. I’m not sure what I am doing wrong. Thank you in advance for any help.


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

public class CameraController : MonoBehaviour
{
    public float speed = 5.0f;
    public float rotateSpeed = 5.0f;
    public float ascendSpeed = 5.0f;
    public float mouseSensitivity = 100.0f;
    private float verticalLookRotation = 0.0f;

    void Start()
    {
        // Lock and hide the cursor
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        // Camera movement
        float horizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        float vertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        float ascend = 0;

        if (Input.GetKey(KeyCode.R))
        {
            ascend = ascendSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.F))
        {
            ascend = -ascendSpeed * Time.deltaTime;
        }

        transform.Translate(horizontal, ascend, vertical);

        // Camera rotation with mouse
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        verticalLookRotation -= mouseY;
        verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f);

        // Apply vertical rotation to the camera itself
        Camera.main.transform.localRotation = Quaternion.Euler(verticalLookRotation, 0, 0);

        // Apply horizontal rotation to the character's body
        transform.Rotate(0, mouseX, 0);
    }
}

The script is okay, you’ve probably just placed the script on the camera when it’s supposed to be on the player.

BTW - you should remove the Time.deltaTime from lines 39 and 40 as the mouse delta is already frame rate independent.