3
\$\begingroup\$

Whenever I use ScreenToWorldPoint(Input.mousePosition) and display the output in console this always gives me the same coordinates.

MyCode:

Vector3 screenToWorld;

void Update()
{
    screenToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Debug.Log(screenToWorld);
}
\$\endgroup\$
7
\$\begingroup\$

If I remember correctly, for ScreenToWorldPoint you need to provide a Z value. This represents a distance to a plane, on which you want to get your resulting position. Try to use this:

new Vector3( Input.mousePosition.x, Input.mousePosition.y, 1.0f )

as your input position.

Here you have Unity example: https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

\$\endgroup\$
1
  • 1
    \$\begingroup\$ kolenda thank you so much it worked. I added z value and it worked mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 4f)); Thank you again kolenda \$\endgroup\$ May 18 '18 at 13:52
1
\$\begingroup\$

To make it properly you should pass the camera's z, like this:

Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mainCamera.transform.position.z)) * -1;

However, I do not recommend you using Camera.main, performance is way better if you keep a reference to the camera.

public Camera mainCamera;

mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mainCamera.transform.position.z * -1));

You should multiply z by -1 here because this will bring you the inverted values of X and Y otherwise.

\$\endgroup\$
1
  • \$\begingroup\$ This form is appropriate if your camera is looking along the z+ axis, positioned somewhere with a negative z coordinate, and you want a point on the world z=0 plane. \$\endgroup\$
    – DMGregory
    May 13 at 20:50
1
\$\begingroup\$

Check your camera projection. It will be perspective. It is because, in perspective view, depth is in the calculation. Since your mouse is in screen space, in simple words your mouse is on the camera itself. So you are trying to convert the camera coordinate into the world. That is why that code will debug the position of the camera itself. So all you need to do is to add a distance from the camera to convert the point.

Vector3 screenToWorld;

void Update()
{
    screenToWorld = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
    Debug.Log(screenToWorld);
}

5f is the distance from the camera plane the coordinates will be converted. Now, this issue will not happen when you switch to orthographic projection in the camera setting as there is no depth calculation required.

A small trick to understand this is to look at the camera gizmo in both the projection. Perspective has a pyramid-like structure where orthographic has a cuboid-like. So the camera origin in the case of perspective is like a point and your mouse is operating on that point so it will only give you the camera coordinate. Isn't it? Now it's just a trick to remember that's all. enter image description hereenter image description here

\$\endgroup\$
0
\$\begingroup\$

The Camera.main.ScreenToWorldPoint() will return the camera's poisiton if you do not include the camera's Z position in the world, if the camera is set up with perspective projection.

For perspective projection:

public Camera camera;

Camera.main.ScreenToWorldPoint(new Vector3(Input.mousPosition.x,
 Input.mousePosition.Y, camera.transform.position.z));

For orthographic projection:

Camera.main.ScreenToWorldPoint(Input.mousePosition);

will work fine.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This form is appropriate if your camera is looking along the z- axis, positioned somewhere with a positive z coordinate, and you want a point on the world z=0 plane. \$\endgroup\$
    – DMGregory
    May 13 at 20:50
0
\$\begingroup\$

This works fine for me:

Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0,0, Camera.main.transform.position.y));
\$\endgroup\$
1
  • \$\begingroup\$ This form is only appropriate when you have a camera looking straight down and you want to get a point on the world y=0 plane. \$\endgroup\$
    – DMGregory
    May 13 at 20:49
0
\$\begingroup\$

Another solution could be:

float zCoord = Camera.main.WorldToScreenPoint(transform.position).z;

Vector3 mousePosition = Input.mousePosition;
mousePosition.z = zCoord;
Vector3 pos = Camera.main.ScreenToWorldPoint(mousePosition);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.