- Home /
How change the camera orthographic size to the UI image top position?
I have an UI image in the scene. What I want is to scale the camera orthographic size to the UI image top position. The red line must be the new camera orthographic size. I really don't know how to achieve this. I first tried this:
float screenAspect = (float)Screen.width / (float)Screen.height;
But it's not working good with some resolutions. It's not precise.
I also tried:
myRectTransform.TransformPoint( myRectTransform.position )
without success. Maybe I set the variables wrong.
Answer by bpaynom · Jan 29, 2019 at 11:12 AM
From Unity Docs (OrthographicSize) The orthographicSize is half the size of the vertical viewing volume.
So, if you know your box is at (x, Y, z ), your box is S unit size and your Camera is at (x', 0, z'), the Orthographic Camera Size would be --> (Y + S/2)
For your UI Image, you cant get the height of your image like this -> rectTransform.rect.height x transform.lossyScale.y .
That is correct. I know that. The problem here is, that is not a box. It's an UI Image. I don't know to calculate it with canvas objects.
But I don't have the ui image position in world coordinates. "myRectTransform.TransformPoint" is not working for me.