- Home /
Implementing Camera bounds proportionally to its ortographic size
Hello everyone I'm currently working on a 2D (or 2.5D rather) game that uses an orthographic camera to display the game's play area; the camera can be panned around and zoomed in and out (achieved by increasing and decreasing its ortographic size). However, I'd like movement of the camera to be restricted to certain bounds so I implemented a simple check as such
if (Input.GetKeyDown(KeyCode.A))
{
print(Screen.width);
if (transform.position.x < 10f)
transform.Translate(-1, 0, 0);
}
The check works well enough, but of course the bounds no longer mean much once the orthographic size has been altered; they need to be adjusted proportionally to how far the camera is "zoomed into" the play area, other wise there's always going to be things that cannot be see unless one zooms out.
I've tried some solutions using the different between the maximum orthographic size and the current one and multiplying that factor by a certain value; it seems to work but of course it's highly dependent on the aspect ratio and it doesn't even work right (it tends to extend the bounds the more zoomed in you are.
if (Input.GetKeyDown(KeyCode.A))
{
print(Screen.width);
if (transform.position.x < 6f + (maxOrthoSize-camera.orthographicSize)*2f)
transform.Translate(-1, 0, 0);
}
Sadly geometry has never been my forte so I have no idea what the best approach to this would be; I tried searching around but haven't found anything that really applies so I hope someone who had encountered a similar obstacle can point me in the right direction
Thanks in advance!
Your answer
Follow this Question
Related Questions
Keeping the camera in bounds 1 Answer
flickering lines when zooming fov (both GUI and ortho cameras). 2 Answers
Make camera Zoom in or Zoom Out to focus all target objects 0 Answers
How to make a smash bros-like camera 3 Answers
How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers