- Home /
[Solved]Orthographic camera RECT based off points on screen
Hello everyone. I dont know whether it is too early/late(no sleep) or I am completely clueless. I cannot figure out how to fix my orthographic camera's rect to match up to points on my main camera's screen.
I have the screen coordinates of all of the blue points. I want to make my orthographic camera fit inside the smaller box. Anyone point me in the right direction for converting these 4 vector3 points into a rect I can use for a camera?
Explanation:
I have a UISprite in Ngui. From that sprite I am grabbing the 4 corners and converting those points to screen space using WorldToScreenPoint. I would like my orthographic camera to fit perfectly inside those 4 points. Thanks!
Answer by robertbu · Nov 03, 2014 at 04:02 PM
First you need just two points, not four. You need the lower left point (llPoint) and the upper right point (urPoint). Second, you need them in Viewport coordinates, not screen coordinates, so you want to use WorldToViewportPoint(). Given those to points, you can do:
Camera.main.rect = Rect(llPoint.x, llPoint.y, urPoint.x - llPoint.x, urPoint.y - llPoint.y);
close, but I needed to divide by the screen height/width as well for the width and height of the rect
but I needed to divide by the screen height/width as well for the width and height of the rect
No, no, no. Viewport points go from (0,0) in the lower left of the screen to (1,1) in the upper right. So if you use WorldToViewportPoint() ins$$anonymous$$d of WorldToScreenPoint(), your calculations will be in the coordinate system used by the Camera.rect. No conversion is necessary. No division by screen width or height is necessary. You are going to a lot of unnecessary work. I'm assu$$anonymous$$g corners[2] is upper right;
Vector3 llPoint = UICamera.mainCamera.WorldToViewportPoint(corners[0]);
Vector3 urPoint = UICamera.mainCamera.WorldToViewportPoint(corners[2]);
mapCam.rect = new Rect(llPoint.x, llPoint.y, urPoint.x - llPoint.x, urPoint.y - llPoint.y);
you are correct, it looks much more clean as well. Thanks, marked as the answer
Your answer
![](https://koobas.hobune.stream/wayback/20220613170634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make WorldToScreenPoint work with an orthographic camera? 1 Answer
How to make WorldToScreenPoint work with an orthographic camera? 0 Answers
Build for Windows : Camera rect always stretched 0 Answers
Changing Ortho Cam Size according to resolution 4 Answers
Keeping two objects in frame 1 Answer