Camera Screen to world point returns cameras transform position.

Debug.Log (Camera.main.ScreenToWorldPoint(Input.mousePosition));

is just giving me the transform of the camera, no matter where my mouse is on screen. Any ideas? Thank you.

As others have pointed out you need Z, which is the distance from the camera. To get the true world point on the cameras viewing plane you should use the clipping plane distance:

Debug.Log(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
  Input.mousePosition.y, Camera.main.nearClipPlane)));

I believe if you use 0 you will just get the camera’s position, as X and Y are irrelevant when scaled to zero. The viewing plane at the cameras origin is a singularity :wink:

Just found out that you need to supply a z position for it to work.

mousePos = Input.mousePosition;
mousePos.z = 1.0;
worldPos = Camera.main.ScreenToWorldPoint(mousePos);

Debug.Log (Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition,0)));

Z element is depth. As Huacanacha says, z = 0 is Camera position and Camera.main.nearClipPlane is the viewport in world space.

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

Did you want

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html

instead?

The input z-coordinate is the key, but it should actually be equal to the Y-axis distance from the Camera to the object being moved. That distance is only equal to Camera.nearClipPlane if your scene configuration happens to have your scene at the Camera’s near clip plane.

void OnMouseDrag() {
	// ScreenToWorldPoint requires distance to Camera as z-coordinate, otherwise it only returns current, fixed Camera position (distanceToCamera = 0)
	Vector3 screenPosition = Input.mousePosition;
	screenPosition.z = Camera.main.transform.position.y - transform.position.y;

	Vector3 worldPosition = Camera.main.ScreenToWorldPoint (screenPosition);
	transform.position = worldPosition;
}

So I had exactly the same code as yours. The bug was that I did set camera position multipler times in multiple scripts. Camera position changed by script X that is should not do, then player target direction computed from camera with ScreenToWorldPoint, then camera position changed to look at player. Result is camera looks at player everything looks fine, but the aiming seems like there is a bug in ScreenToWorldPoint. Tried disabling random scripts and it worked. That way I found out which one does it.

Still not working for me, the z is different to the camera’s z, but the object still follows the camera’s x and y instead of the mouse.

Setting your camera to orthographic (if appropriate for your game) will also solve this.