- Home /
How do you clamp a camera by mobile width?
I need to prevent my camera from going beyond certain stage boundaries - however, I'm designing for Android and variable screen resolution. A basic Mathf.clamp won't do the job. I know I need an offset based on the screen width... perhaps offset = screen.width / 2? What is the best way to go about this?
Okay.. figuring that a image of the issue may help. My mistake for phrasing it as resolution, but this isn't about placing GUI items - that's already taken care of.
The clamp is based on the center of the camera, local 0,0,0. X is being clamped, not the entire vector. If I base the clamp off a single ratio, then ratio 2 will see beyond the stage edge, thus the need to figure out the offset from "X".
Answer by robertbu · Jun 03, 2013 at 08:01 PM
If the object is at a fixed plane distance from the camera, then you can use Viewport coordinates. Viewport coordinates start at (0,0) in the lower left of the screen and go to (1,1) in the upper right. So if the plane the objects move on is 10 units in front of the camera, you could convert do:
var LowerLeft = Camera.main.ViewportToWorldPoint(Vector3(0.0,0.0,10.0));
var UpperRight = Camera.main.ViewportToWorldPoint(Vector3(1.0,1.0,10.0));
Now you have the maximum and minimum X and Y coordinates in world space that are visible at at a distance of 10 units in front of the camera. Note you may have to do more, because the calculations just say that a specific position is visible or not. Your objects have width. So you might end up using (0.1,0.1,10) and (0.9,0.9,10.0), or if things are really critical, using renderer.bounds to figure things out.
How would that help me clamp the camera though? This is on a 2D plane - it's directly related to the last question I posted, but.. $$anonymous$$athf.clamp is a float. $$anonymous$$y goal is to devise a clamping method to prevent all screen resolutions for mobile devices from seeing "beyond" the stage of the game.
Huh? Changing the field of view or orthographic size? You're missing it again. This is for dedicated Android devices. Yes, I'd change the aspect ratio to test the offset, but in play on an actual device, you can't do that. The point is to get the offset dynamically so the view can be clamped to the map edges. While this is - and I repeat - NOT a clone of the game, I'll point to Bad Piggies as an example, where you never see past the edge of the map no matter what device you are on.
Reading again this is what I get. You have a "stage" which is bigger than is viewed by the camera, but has limits. The user can move the camera (or something track the action and move the camera), but you want to limit the camera so that the user can never see beyond the stage. Now I'm going to make three assumptions:
The stage is at a fixed distance from the camera.
You have the bounds in world space of the stage.
You are looking down the 'Z' axis so that positive X is to the right and positive 'Y' is up.
So at start, you calculate the world coordinates of the lower left and upper right of the camera viewport (in world coordinates) as I've indicated above. Then you can calculate $$anonymous$$ and max values for the camera.
var viewportWidth = UpperRight.x - LowerLeft.x;
var viewportHeitht = UpperRight.y - LowerLeft.y;
var $$anonymous$$X = stageLowerLeft.x + viewportWidth / 2.0;
var maxX = stageUpperRight.x - viewportHidth / 2.0;
var $$anonymous$$Y = stageLowerLeft.y + viewportHeight / 2.0;
var maxY = stageUpperRight.y - viewportHeight / 2.0;
You can then use these values in a $$anonymous$$athf.Clamp() to make sure the camera never leaves the stage.
I solved it, and with just two variables.
leftOffset = 0 + ((Screen.width/22)*100)/215;
rightOffset = 215 - ((Screen.width/22)*100)/215;
This is dividing the viewport by the depth to get a percentage based on the total stage "length", then adding or subtracting to either side of the stage as needed. And it works.
Answer by Owen-Reynolds · Jun 03, 2013 at 08:41 PM
It isn't so much screen resolution (pixels) as it is screen relative dimensions (Aspect Ratio.) If you make a game that fits on a screen 1.5 times as wide as it it tall (3:2 ratio) it will fit the same on a 300x200 screen, or a 3000x2000.
Exceptions are placing GUI-items using pixels. The trick is to always place and size them using screenPct (ViewPort coords, in robert's reply.) Lots of questions&answers here about doing that.
If you look at various non-Unity Android-phone stuff, that's what they're getting at. Instead of counting pixels, try to say stuff like "1/4 of the screen."
Your answer
Follow this Question
Related Questions
why my unity games run on android devieces,the screen will be wrong. 0 Answers
Detect if Android screen is widescreen? 2 Answers
Android - Camera orientation 1 Answer
How can I fix the screen disturbance of android 4.1/4.2 ? 0 Answers
(Android) Pick up objects with the cross from the centerof the screen 1 Answer