ScreenToWorldPoint giving inconsistent numbers?

Im running this snippet of code and i think it is giving me inconsistent values. I’m working with a 1920 x 1080 screen and the Vector 3 data, fullscreen.x is sometimes 8.95 and sometimes 7.25. it changes from randomly from time to time every time i build for my android phone. The variables are global so don’t need to worry about that.

void Start(){
		x = Screen.width;
		y = Screen.height;

        dist = transform.position.z - Camera.main.transform.position.z;

		//if width somehow becomes smaller than height, swap
                //just a fancy way of swapping w/o a temp variable
		if (x < y) {
			x = x ^ y;
			y = x ^ y;
			x = x ^ y;
		fullScreen = Camera.main.ScreenToWorldPoint(new Vector3(x, y, dist));

The ‘z’ parameter in ScreenToWorldPoint() is the positive distance in front of the camera for the calculation. The only time this makes sense:


…is if the camera is looking towards negative ‘z’ and the point you do the calculation is at ‘z = 0’. It is unlikely this is how your scene is setup. If the camera has no rotation and the object with this script is at the distance you want to calculate, you can use:

  float dist = transform.z - Camera.main.transform.position.z;
  fullScreen = Camera.main.ScreenToWorldPoint(new Vector3(x, y, dist);

Your fancy swap seems like a waste of some cycles to me just to avoid a temporary variable.

The problem is that during the VERY START of the game, the orientation is always defaulted to portrait before it is detected that the resolution must be in landscape. I think i read somewhere that resolution is only changed after the first update() is called. Therefore, I delayed the code to retrieve world units by 0.5 seconds and now it is returning consistent numbers.

I had a similiar issue. The core problem was the values in Camera.main being updated after the values in Screen.width. The fix was checking for changes in the Camera.main.aspect ratio in Update(). When that change is detected is when I did the Screen.width & Camera.main based computation.