Camera continuous rotation after hitting object

I cant for the life of me figure out why but my camera script runs fine, until I hit a small object then it starts rotating continuously in either left or right. Feel I should also mention Im very new to coding and unity and this code was from a video.

This is the camera script if the problem lies in here.

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

public class MouseLook : MonoBehaviour
{


    public float mouseSens;
    public Transform playerBody;
    float xRot = 0f;
    // Start is called before the first frame update
    void Start()
    {
       Cursor.lockState = CursorLockMode.Locked; 
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSens * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSens * Time.deltaTime;

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

        transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);

    }
}

Good day. This is a “standard response”

For this kind of problems, you need to learn to find your problem by your own. As you can imagine, we can not try to read all scripts people posts, understand the logic and process, what all variables means, when or how you use them, and find where is the “problem”. You are the only one who can do it…

You need to debug the code while running, and check the states of all variables at the moment you see the problem, and I’m sure you will detect what is not how it was suposed to be.

If don’t know how, look for some tutorials on how to DEBUG your code while running.

Bye & good Luck!