Problems with rotating camera to mouse pointer using Transform.LookAt and other similar methods

I’ve been trying to make the camera move with my mouse pointer, so that I can look around with the camera. I’ve tried multiple ways of doing this although all of them were using Transform (using Rotate and rotation but I found LookAt to be the simplest and has the same results) . And I keep on getting the same problem, the camera never wants to rotate at the y axis, not without glitching out at least. X axis works decently fine, I know this code isn’t perfect and awfully simple but I’m still dumbstruck to why the y axis isn’t rotating at all like the x axis is. I have no clue at all to why this is happening.
It’s probably due to my horrible code and horrible knowledge of unity (and coding in general) but help would be very much appreciated… as I have no clue to why this isn’t working. (A reply that explains why and tells me how I’m wrong is also very much appreciated!) Also it might be important to mention that the “main camera” is a child to the “player” (which is where the script is assigned to), and the “player” it’s self is a child to “PlayerMain” which is just an empty gameObject. Thanks for all help!

public class LookAroundScript : MonoBehaviour {

    //Making variablas with type of Transform 
    Transform cameraTransform;
   
    //Getting the GameObjects
    public GameObject theCamera;

    Vector3 mouseCord;
    void Update()
    {
        //Getting the transform compenent
        cameraTransform = theCamera.GetComponent<Transform>();
          
        //Recording the position of the mouse
        mouseCord = Input.mousePosition;
        //Seeing the coordinates
        Debug.Log(mouseCord);

        //Making the camera rotate to the mouseCords
        cameraTransform.LookAt(mouseCord);
        
        }
    }

This is a problem of mismatched coordinate spaces.

Input.mousePosition returns a Vector3 in a range from [0,0] to [screenWidth, screenHeight], and is measured in pixels. This coordinate space is known as Screen Space, and it represents measurements on the flat plane of the screen.

Transform.LookAt takes in a Vector3 representing a position in the world, otherwise known as World Space.

Because your Vector3 mouseCoord doesn’t contain any information about what coordinate space it was generated from, there is no way for Transform.LookAt to convert to the appropriate coordinate space.

There are a few solutions to this that are pretty standard practice. The most simple way to rotate the camera with the mouse is to track its change in screen space position each frame, and use those values to generate a Vector3 representing how much to rotate in each axis. Change in x position = y rotation, change in y position = x rotation.

So your code would look like

Vector3 lastFrameMousePos;

// This value is basically what converts from pixels to degrees (or radians)
// There's no direct conversion so we can just fudge it until it feels good.
public float RotationScale = .1f; // Play with this value until the rotation speed is what you want

void Awake () {
    lastFrameMousePos = Input.mousePosition;
}

void Update () {
    Vector3 deltaMousePos = Input.mousePosition - lastFrameMousePos;

    cameraTransform.Rotate (deltaMousePos * rotationScale);

    lastFrameMousePos = Input.mousePosition;

}