How to get screen bounds in Unity?

In other game engines, I’d do something like this to keep an object in bounds:

if (x < 0) x = 0;
if (x > SCREEN_WIDTH) x = SCREEN_WIDTH;
if (y < 0) y  = 0;
if (y > SCREEN_HEIGHT) y = SCREEN_HEIGHT;

Of course this doesn’t work in Unity, as the origin isn’t the top left corner of the screen, and camera placement changes all the values anyhow. How do you determine the top, bottom, left, and right screen bounds?

Well, there is: Screen.width and Screen.height which gives you the screen resolution in pixels.

But probably what’s more important is where you’re getting your x y values from in the first place. If they’re the x y values from a 3d position in space, they’re not going to be very useful in terms of restricting your object to the visible area.

Perhaps you should post some more details of the type of game you’re making, and maybe we can come up with a more specific solution.

Well this isn’t about any one game; it’s about me learning the right way to do things in Unity. I pretty much relied on brute force and trial and error to get thru my first project.

I’m doing 2D games on the iPhone so I already know the screen dimensions. But the camera position may change over time… so I need to know the top/bottom/left/right bounds of the screen at any given time.

If you’re using an orthographic camera for a 2d game view, you can specify the size of the camera view using camera.orthographicSize.

Knowing the orthographic size of the camera, It’s then just a case of adding (or subtracting) that value to the camera transform’s position to find out the vertical view bound. As it says on that page, the horizontal view bounds will be correspondingly determined by the viewport’s aspect ratio.

Thanks, but I’m not currently using orthographic cameras.

I wonder if Camera.pixelWidth and Camera.pixelHeight will work?

Something like:

leftBounds = Camera.x - (Camera.pixelWidth/2);

Well, the problem you have with non-orthographic cameras is that there’s no single value for your camera bounds in a given direction. Because of perspective, the further away an object is, the further it can travel left or right before it disappears from view.

So, even though your game is “2D”, your graphics are being displayed at a certain specific distance away from the camera, and therefore the “camera bounds” for those graphics are dependent on the distance between the camera and the graphical object’s world position.

The Camera class has a number of functions for converting between screen-space and world-space.

For instance, you can get values that might be usable as your camera bounds by using camera.ViewportToWorldPoint for each of the four corners of your viewport rect. When using this function though, you need to specify how far from the camera you are talking about (specified as the z value of the vector3 that you pass). So, for that value you’d provide the distance between your camera and your game’s 2D play area plane.

Hope this points you in the right direction.

Thanks, I believe camera.ViewportToWorldPoint is what I needed!

Hmm… camera.ViewportToWorldPoint works fine with orthographic cameras, but doesn’t appear to be working with non-orthographic cameras.

Never mind, I think I’ve got it working now!

Maybe this links will help you, it explain different perspective camera tricks

http://docs.unity3d.com/Documentation/Manual/FrustumSizeAtDistance.html
http://docs.unity3d.com/Documentation/Manual/CameraTricks.html

2 Likes