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”.
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.
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.”