ScreenToWorldPoint. Frustration

Hi guys.
Just to sum it up. I tried multiple approaches by now but nothing worked neither in terms of funtionality neither of sense for my use. However Screentoworldpoint seemed pretty sufficient so i tried. Well. All I get is the main Cameras position no matter where i position my mouse. (even after copying others code).
Maybe someone sees what the problem is…

Vector3 desiredposition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        desiredposition.z = 5f;
        Debug.Log (Input.mousePosition+"|"+desiredposition);

So from the documentation:

Note how the example code in the docs even sets the z part of the vector that goes into the ScreenToWorldPoint method:

var p: Vector3 = camera.ScreenToWorldPoint(new Vector3(100, 100, camera.nearClipPlane));

The z part is used to define which plane you want the mouse position on… or rather, how far in front of the camera.

With out knowing how far in front of the camera you want the point, there’s no way to get a point… there are infinite points under the mouse pointer. Points near, and points far, stretching out into infinity… this is the nature of a 2d image of a 3d world.

By passing 0 as the z, you’re saying on the plane 0 units in front of the camera. Well that plane the position of the camera, and the planes size is a mere dot, because view frustum starts at that point (if you were in orthographic projection, this would be different… this is maybe why some example code you borrowed from other users worked for them).

So get the mouse pos, and set the z to how far in front of the camera you want the point.

var mousePos = Input.mousePosition;
mousePos.z = 5f;
var desiredPos = Camera.main.ScreenToWorldPoint(mousePos);
2 Likes

Thank you very much used this in my code. Resolved some problems :slight_smile: