- Home /
How does ScreenToWorldPoint work with split screen with multiple cameras?
I have three orthographic cameras on that I use to split the screen in the following way:
So before I split the screen, I had one game running that was spawning objects randomly from the top of the screen (top of Y axis/screen) and randomly on the X axis. When I had 1 camera, I used to get the screen boundaries like this:
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
Then to generate the position of the dynamically generated object I did:
float spawnX = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
Vector2 spanwPosition = new Vector2(spawnX, screenBounds.y);
spanwPosition.x = Mathf.Clamp(spawnX, screenBounds.x * -1 + objectWidth, screenBounds.x - objectWidth);
The last part is correct the random X position of the spawned object not be half outside the screen. My problem is that now when I have 3 different cameras, I can find the their boundaries by:
float height = 2f * gameCamera.orthographicSize;
float width = height * gameCamera.aspect;
screenBounds = gameCamera.ScreenToWorldPoint(new Vector3(width, height, gameCamera.transform.position.z));
but the above code for generating the position of the object does not longer work and the objects are spawning way off the camera in the world space (I am of course adjusting the camera in it). So my guess is that the starting position (0,0) for the ScreenToWorldPoint method isn't correct because maybe the (0,0) of the screen isn't on this camera? So then what is the starting point of my camera view?
I mean, I managed to do what I wanted with ViewportToWorldPoint but how does one work with ScreenToWorldPoint in such a situation with multiple cameras?
Answer by Namey5 · Jun 18, 2020 at 04:00 AM
A couple of things.
'ScreenToWorldPoint' works using the camera's individual projection matrices and therefore resolution. When a camera covers the entire screen, Screen.width/height are identical to Camera.pixelWidth/pixelHeight. However, if your camera doesn't take up the entire dimensions of the screen then using the Screen resolution won't work. As such, it's generally more advisable to use your camera's dimensions over the screen's (and [0,0] is still the other corner regardless).
Something else to note is that the z component of the input vector to 'ScreenToWorldPoint' is a view-space depth, not the camera's actual world-space z coordinate. As such, you should pass in the distance from the camera at which you want the objects to be placed.
//You should cache this as every access of Camera.main actually does a full search for the camera
Camera cam = Camera.main;
//Use the camera's dimensions and get a point 5 metres away from the camera (you could use cam.nearClipPlane as the z-coord to get a point at the camera's position)
screenBounds = cam.ScreenToWorldPoint (new Vector3 (cam.pixelWidth, cam.pixelHeight, 5f));
"(and [0,0] is still the other corner regardless)." Could you please elaborate on that? I didn't quite understand it. Anyways: Thanks a lot for the nice comment and help! You not only explained everything very well, but also gave me some cool tips about stuff that I did not know, like how Camera.main functions and that I should cache it, or about pixelWidth and pixelHeight, which I didn't even knew existed. So are the "camera's dimensions" actually those 2 parameters: pixelWidth and pixelHeight?
Yes, Screen.width/height is simply the resolution of the window the game is running in, whereas Camera.pixelWidth/pixelHeight is the actual resolution of that specific camera. As for [0,0], what I mean is that when you are working with screen space coordinates (like in ScreenToWorldPoint), the bottom left corner of the current camera's display is at coordinates [0,0] and the top right corner is at coordinates [Camera.pixelWidth, Camera.pixelHeight].