- Home /
Resizing orthographic camera to fit 2d sprite on screen
Hello!
I have a game object made up of multiple sprite game objects. Each sprite is a "block" (think terraria / minecraft) and the main gameobject is a collection of these blocks. I need my orthographic camera (it's a 2d game) to shrink it's width according to the width of the game object. Basically, I need to do the opposite of what orthographic.size does. The height of the orthographic camera doesn't matter, but the width does. The main game object that consists of the "ground blocks" must always fill the screen width, the height can then stretch as far as it needs for it to be proportional. I've searched for something like "orthographic width", but found nothing. How would I go about doing this?
Hope my explanation isn't too muddy.
Also, if you have a suggestion on how to better solve this problem than resizing the width of the camera according to the main game object, please say so.
Thanks!
P.S Basically, I understand what I probably need to do: calculate the orthographic size depending on the width of the game object that I want to fit on the screen. However, I can't figure out the maths to do this.
Answer by robertbu · Jul 31, 2014 at 01:56 PM
Orthographic size is 1/2 of he vertical size seen by the camera. So try this:
Camera.main.orthograpthicSize = widthToBeSeen * Screen.height / Screen.width * 0.5;
be sure to convert to float, or you may get 0.
((float)Screen.height / (float)Screen.width) * 0.5f
Your answer
Follow this Question
Related Questions
Why is part of object outside camera frustum visible? 1 Answer
Making a camera semi-orthographic 2 Answers
2d orthographic camera follow 1 Answer
ScreenToWorldPoint not accurate 0 Answers
2D camera zoom in comparison to the height of target 2 Answers