# Why is ScreenToWorldPoint always giving same coordinates with mouse position?

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);
}


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 )


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

• 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 May 18 '18 at 13:52

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.

• 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. May 13 at 20:50

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.

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.

• 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. May 13 at 20:50

This works fine for me:

Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0,0, Camera.main.transform.position.y));

• 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. May 13 at 20:49

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);