Reputation: 24688
I'm writing a 2d game in unity and would like to force the orthogonal camera's view frustum to be altered only on the right-hand side according to the device's aspect ratio, with the left/bottom being set to 0,0
i.e.: I'd like the bounds of the camera's Y frustum to be to be 0 at the bottom and 10 at the top. That much is easy and I've done it by moving the camera's position to 5 and setting the camera size to 5 also (I'm presuming that has worked). (also, there's no real reason why I've gone for 10, it just seems like a round number...)
When the scene starts up I'd like to move the camera's X frustum so that the left is 0 and the right is whatever the aspect ratio demands: somewhere between 4:3 and 16:9 depending on the device. That will pretty well just mean setting the camera's x translation I presume, but I'm a touch lost on how get the aspect ratio, and then to calculate what it should be?
Upvotes: 1
Views: 3872
Reputation: 24688
...and here's the answer:
Vector3 pos = new Vector3(5.0f * Camera.main.aspect, 5.0f, -10.0f);
Camera.main.gameObject.transform.position = pos;
Which I popped into my main.cs script, under the Start() function. The 5.0f is half the height of my 2d camera'a othrogonalSize (as mentioned in the question above), and the -10.0f is the default z-position for the main camera in a 2d game.
Upvotes: 1