Setting positions of points on Line renderer to mouse position does not work

Hello there, I have a problem that has been completely stumping me for days at this point.

I am trying to draw lines with the Line Renderer with the mouse, now this works for an orthographic camera but for my game I need the camera to be perspective. However, the drawing completely breaks when setting the camera to perspective.

When it is perspective and I try and draw, the positions of the points are offset from where the mouse actually is. I have tested and I am pretty sure the Raycast is returning the correct mouse position, but at this point I have no idea. It also seems like the farther I get from the center of the camera the larger the offset from the mouse to where the point is placed is:

When I click it places the first point, in the image I clicked and you can see the point was placed away from where the mouse actually is

Heres another:

As you can see this time the point is considerably farther away from the mouse, as I said it seems as if the farther I am from the center point the worse the offset is. My thinking is that maybe this is a hint that the perspective is causing the offset but the mouse position, again, is correct when I Log it. The green line is also attempting to draw the points at the mouse’s positions but it is also completely off.

This is literally all I am doing to get the mouse’s position and it works perfectly fine in orthographic mode:

Vector2 CheckMousePosition()
    {
        // Create a ray from the screen to the world, if anything is hit then return the point
        // TODO: Discern the whiteboard from the rest of the world so they can't draw on anything but the whiteboard
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            return hit.point;
        }

        // Return the last recorded mouse position so that the line isn't updated 
        return temporaryLineMousePositions[temporaryLineMousePositions.Count - 1];
    }

And this is all I am doing when setting the point on the green line:

temporaryLineMousePositions.Add(newMousePosition);
temporaryLineRenderer.SetPosition(1, newMousePosition);

I have no idea where the problem could be, everything else in my code wouldn’t have an effect on the position these are the only places I can think that are causing it. If anyone has any ideas it would be very appreciated, thank you

I had this problem too when I switched my camera from Orthographic to Perspective.

This answer solved my problem:

(in case the link breaks, here’s the code that I use)

public static Plane raycastPlane = new Plane(Vector3.forward, Vector3.zero);
public static Vector2 ScreenToWorldPoint(Vector3 screenPoint)
{
    Ray ray = Camera.main.ScreenPointToRay(screenPoint);
    float distance;
    raycastPlane.Raycast(ray, out distance);
    return ray.GetPoint(distance);
}