- Home /
Orthographic cameras and screen resolutions
Hi,
This was originally posted as a question on the forums, but sank without a trace and with no responses, and I thought it might be better suited to unityanswers instead.
I've had a good trawl through various forum posts and other questions on this, which has partly helped me to get to the point I am at now. I am trying to create a simple window using an orthographic camera where the bottom left corner has coordinates of (0, 0, 0) no matter what the actual window size/screen resolution. So far I have produced the following script:
function Start() { resolutionCounter=0;
Screen.SetResolution(Screen.resolutions[0].width, Screen.resolutions[0].height, false);
Camera.mainCamera.orthographicSize = Screen.height/2; Camera.mainCamera.transform.position = new Vector3(Screen.width/2,Screen.height/2,-100);
referenceObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); referenceObject.transform.position = new Vector3(10,10,10); referenceObject.transform.localScale = new Vector3(10, 10, 10); }
function Update () {
if (Input.anyKeyDown)
{
if (resolutionCounter<Screen.resolutions.Length-1)
{
resolutionCounter++;
}
else
{
resolutionCounter=0;
}
Screen.SetResolution(Screen.resolutions[resolutionCounter].width, Screen.resolutions[resolutionCounter].height, false);
Camera.mainCamera.orthographicSize = Screen.height/2;
Camera.mainCamera.transform.position = new Vector3(Screen.width/2,Screen.height/2,0);
}
}
This code allows me to cycle through the supported resolutions properly. However, it doesn't keep the created sphere 10 pixels from the bottom and left of the window, which I thought should be the case no matter what resolution is chosen. In fact, the sphere jumps all over the place, and is sometimes not in the window at all.
Am I doing something wrong here, or is my understanding of what should be happening incorrect?
Thanks,
Wibbs
not had a chance to try your script but I'm pretty sure you don't want the lines in which you transform/move the camera. Try commenting them out and see what happens, also switch between your Scene and Game tabs to see what's going on.
I'm not at my computer at the moment so can't check this, but isn't it the move/transform that ensures the camera is centred in the 'active' area?
Unfortunately that does not seem to solve the problem, so I'm still stumped :(
O$$anonymous$$, I can refine the question a little now. What I am trying to do is get a 1 to 1 mapping between world space and camera space. I've added test code to print out points in both, and it clearly isn't working, but I don't know why. Any ideas?
Answer by Wibbs 1 · Mar 15, 2011 at 06:57 PM
Okay,
I've found the answer to this. Screen.width and Screen.height seem to lag behind and were not being updated in time for changing the camera's orthographic size or position in the same Update call. I replaced these lines with:
Camera.mainCamera.orthographicSize = Screen.resolutions[resolutionCounter].height/2;
Camera.mainCamera.transform.position = new Vector3(Screen.resolutions[resolutionCounter].width/2,Screen.resolutions[resolutionCounter].height/2,-100);
and now everything works as expected, with world space giving the same x,y coordinates as camera space.