- Home /
How to stop a camera panning at level boundaries while zooming?
Apologies if this has been asked before, but it's slightly different to what I kept finding when I tried searching.
I have a camera that follows a character in a 2D platformer style game. I already know a simple way to stop it moving past the level boundaries normally is to use MathClamp on its position.
But there's one point of the level where the orthographic camera zooms out as the character climbs steps, making it harder to pinpoint exactly what position the value should be clamped at.
Is there a simple way to find the X and Y values of the screen boundaries or should I just find it by trial and error, editing values?
Answer by robertbu · May 21, 2013 at 05:52 PM
Convert viewport coordinates into wold coordinates. Viewport coordinates start at (0,0) in the lower left of the screen and reach (1,1) in the upper right. You need to know the distance between the camera and the point you want to convert which should be easy with a 2D game. The Z parameter of ViewportToWorldPos() gets the distance from the camera, and this code uses 10. Replace with your actual distance:
var v3WorldPosLL = Camera.main.ViewportToWorldPoint(Vector3(0,0,10));
var v3WorldPosUR = Camera.main.ViewportToWorldPoint(Vector3(1,1,10));
With these two positions, you can calculate the other two points as well as get the height/width if needed.
Answer by deek0146 · May 21, 2013 at 06:06 PM
Say your level size is the Vector2 Ldim.
The camera at it's current level of zoom is able to see rectangular area of the level Vdim. You can calculate Vdim using the distance between the camera and the level, and the view frustum of the camera. There are utilities in the Camera component to help with this, if you need me to explain exactly how to do this, comment and I will do so.
Your camera needs to be bound to a plane whose dimensions is equal to Ldim - (Vdim*0.5f)
Your answer
Follow this Question
Related Questions
Apple like camera Panning(Finding mouse speed and apply it to camera) 0 Answers
Pan Orthographic camera in 3D 4 Answers
2dToolkit and Physics2D.OverlapCircle 2 Answers
Inconsistent 2D collision detection since 5.2.3 1 Answer
Deactivate Key. 2 Answers