- Home /
2D Orthographic camera movement clamp in any orthographic size
Hello guys.I have a problem, which i currently can't figure out how to solve. I am trying to calculate a clamp value for world coordinates which have to respect any camera orthographic size.I bought an image file just to illustrate it better. Picture 1 and 2 are my current results which i get from the camera extents.Picture 3 and 4 illustrates the problem i need to solve.So for example if i lower a little bit the camera orthographic size, if the clamp values stay the same, there is a space between the camera view and the world map which can't be covered by the clamped values in this current condition.So is there any way i can calculate the clamped positions in every camera orthographic size?Can somebody help me out?
Answer by robertbu · Jul 17, 2014 at 02:55 PM
Orthographic.size is 1/2 of the world height seen by the camera. So in order to generate the values:
var minY = Camera.main.transform.position.y - Camera.main.orthographicSize;
var maxY = Camera.main.transform.position.y + Camera.main.orthographicSize;
Ah yes i was not sure if this was the right answer, but now i see it is somehow contributing to me to get the final result. When i get the camera bounds like robertbu wrote i then get the $$anonymous$$imum and maximum positions of the map in world space and then apply the clamp. Thanks!
I guess reading back your question, I did not give you what you indicated in the first line. Here are two answers (one use OrthographicSize and one using viewport coordinates) for this problem:
http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html
http://answers.unity3d.com/questions/468253/how-do-you-clamp-a-camera-by-mobile-width.html
I believe that both assume that the map is centered at the origin. It would take a bit more work if the map is not centered on the origin.
Your answer
Follow this Question
Related Questions
How can I clamp a GameObject to screen borders? 2 Answers
Constrain a GameObject's movement to an orthographic camera's viewport bounds 4 Answers
Moving a Rotated Camera 0 Answers
Camera Zoom script stops ability to pan 1 Answer
Linecast not working when main character controller is not moving -1 Answers