Converting Pixel Space to World Space

I am trying to convert the position of a UI Selection Box, into world coordinates, so that I can select items which correspond with world space positions.

For some reason, my example function below shows that coords1 and coords2 are not identical positions, however once they are converted to ScreenToWorldPoint, they both take on an identical position of the cameras gameObject.

mousePos1 is a variable which is set earlier on, when the left mouse button is downed, once the button is released coords2 is set to the new mouse position.

void GetSelectionBoxCoords()
    {
        Vector3 coords1 = mousePos1;
        Vector3 coords2 = Input.mousePosition;

        Debug.Log("coords1 is " + coords1.ToString() + " and coords2 is " + coords2.ToString());

        Vector3 coordsToCam1 = myCam.ScreenToWorldPoint(coords1);
        Vector3 coordsToCam2 = myCam.ScreenToWorldPoint(coords2);

        Debug.Log("coordsToCam1 is " + coordsToCam1.ToString() + " and coordsToCam2 is " + coordsToCam2.ToString());
    }

Why is ScreenToWorldPoint producing the exact value of the cameras location? Can anyone help. I am using Unity 5.6.

Thank you

Due to perspective, screen positions don’t relate directly to world positions. For example, the pixel at the left side of your screen could relate to:

  1. Position of an object that’s slightly to the left of your camera but very close

or

  1. Position of an object that’s very far to the left of your camera but far away

To accommodate this the ‘ScreenToWorldPoint’ uses the Z value of the Vector3 you pass to work out the correct world position. You could use the Z value of the 3d boxes you want to check against to get a good approximation.

p.s. To answer your original question, the value returned is the camera position because your Z value is 0.0