🤔

ScreenToWorldPoint#

ScreenToWorldPoint takes a Vector3 argument, where the first two values are screen coordinates, and the third is the distance from the camera.
Providing distance is important when using a perspective camera, as 0 distance will return the world space camera position.

Resolution#

Provide a distance in the z coordinate of the screen position.

float distance = camera.nearClipPlane;
Vector3 worldPoint = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance));
information

If appropriate, switch your camera to Orthographic projection. This is the correct approach when making entirely 2D games.

Interactive Diagram#

Drag the point to modify mouse position, move the slider to change the distance.

Alternate methods#

Using a Plane#

Using a Plane and ScreenPointToRay avoids having to calculate an appropriate distance from the camera. When the result is on a fixed plane in space, this can be a suitable approach.

Plane plane = new Plane(Vector3.up, Vector3.zero); // Provide your own plane
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out float distance))
{
Vector3 worldPoint = ray.GetPoint(distance);
// Use worldPoint
}
You must provide your own plane. Note that the Plane constructor is normal, point.

Using Physics#

Using a Raycast and ScreenPointToRay avoids having to calculate an appropriate distance from the camera. When the result is meant to be on the surface of colliders, this is the correct approach.

Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
    // Use hit.point
}

The position returned is still incorrect.