- Home /
Not getting expected result for ScreenToWorldPoint,Trying to understand Camera.ScreenToWorldPoint
I have setup a scene to include a camera with the following transform data:-
position 0, 6.93, 0;
rotation 90, 0, 0;
scale 1, 1, 1
I have a sphere in my scene with the following transform data:-
position 0, 0.54, 0;
rotation 0, 0, 0;
scale 1, 1, 1
All of this means that I have the camera looking down at the sphere.
The sphere in the scene has the following script attached to it:-
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
var camera = Camera.main;
Debug.Log(Screen.width);
Debug.Log(Screen.height);
Vector3 topLeft = camera.ScreenToWorldPoint(new Vector3(0, 0, camera.nearClipPlane));
Vector3 topRight = camera.ScreenToWorldPoint(new Vector3(camera.pixelWidth, camera.pixelHeight, camera.nearClipPlane));
Debug.Log(topLeft);
Debug.Log(topRight);
}
// Update is called once per frame
void Update()
{
}
}
Now, when this runs I get the following values:-
topLeft: (-12.3, 6.6, -5.0) bottomRight: (12.3, 6.6, 5.0)
If I set the position of the sphere to those coordinates, it is no where near the scene and the y axis has the sphere very close to the camera. Any help would be appreciated.
Your answer
Follow this Question
Related Questions
Need explanation for this code 1 Answer
How does ScreenToWorldPoint work with split screen with multiple cameras? 1 Answer
Need help putting an object at a point based on screen coordinates 1 Answer
Camera space conversions not always working with Pixel Perfect Camera 0 Answers
mouse position on terrain 1 Answer