- Home /
Calculating 2D camera bounds
I'm writing a very simple top down game, I am using an orthographic camera looking at a 1x1 cube that will represent the player and a 100x100 cube that the map is put on.
I am currently working on bonding the camera so that it cannot pass the edge of the cube on which the map is textured. I looked into using WorldtoScreenPoint to find where the edges of the 100x100 cube were but they didn't really make sense, I assume since it's 100x100 positioned at 0,0 it should be 50,50 and -50,-50 but I always get weird divisions. (I am using an orthographic camera that is 1 unit away from map/player.
Is it possible to calculate where the borders of the cube should/are so that I can bound the camera within them?
Debug.Log(Camera.main.WorldToScreenPoint(GameObject.Find("map").transform.position));
returns (2485.4, -1793.1, 1.0) at the top left conrner
in the transform of the cube is the position set to (0,0,0)?
Answer by robertbu · Jul 27, 2013 at 07:34 PM
WorldToScreenPoint() is not what you need to solve this problem. Screen coordinates are pixels on the screen starting at 0,0 in the lower left. So your call above took the center of the cube and translated it into a screen position. The fractional amounts are due the fact that the real-world coordinates of your cube rarely land exactly on a pixel. Judging by the numbers returned, your camera was off in the corner of your map.
So to solve your specific problem, take a look at Camera.orthographicSize. It is half the vertical size seen by the camera. So if you have a cube that is 1x1x1 with your camera centered on it, you could set your orthographicSize to 0.5, and vertically it would just fit on the screen. The distance between the camera and the cube does not matter. The resolution of the screen you are playing on does not matter. If you set orthographicSize to 50(and centered the camera), then vertically your map would just fit the camera. We can calculate how much the camera sees horizontally by:
horzExtent = Camera.main.ortiographicSize * Screen.width / Screen.height;
'horzExtent' will be 1/2 of the horizontal size the camera sees in World coordinates.
So if you want to limit your camera to seeing just your map you can do something like this to your camera script:
#pragma strict
var mapX = 100.0;
var mapY = 100.0;
private var minX : float;
private var maxX : float;
private var minY : float;
private var maxY : float;
function Start() {
var vertExtent = Camera.main.camera.orthographicSize;
var horzExtent = vertExtent * Screen.width / Screen.height;
// Calculations assume map is position at the origin
minX = horzExtent - mapX / 2.0;
maxX = mapX / 2.0 - horzExtent;
minY = vertExtent - mapY / 2.0;
maxY = mapY / 2.0 - vertExtent;
}
function LateUpdate() {
var v3 = transform.position;
v3.x = Mathf.Clamp(v3.x, minX, maxX);
v3.y = Mathf.Clamp(v3.y, minY, maxY);
transform.position = v3;
}
Thanks! This is perfect, i didn't realize i could extrapolate between the orthographic camera size and the map co-ordinates... this is super useful!
You have a typo in your first line of code, it should be orthographicSize ins$$anonymous$$d of ortiographicSize
You are genius man,,, thats code really help me :) thx
I love this community! 6 years after this comment was posted it is still helping people. This was a lifesaver. Or at least a phenomenal time saver!
Answer by GeekyMonkey · Aug 28, 2014 at 02:54 AM
This is my Camera extension class function to get the orthographic camera bounds in world space:
public static class CameraExtensions
{
public static Bounds OrthographicBounds(this Camera camera)
{
float screenAspect = (float)Screen.width / (float)Screen.height;
float cameraHeight = camera.orthographicSize * 2;
Bounds bounds = new Bounds(
camera.transform.position,
new Vector3(cameraHeight * screenAspect, cameraHeight, 0));
return bounds;
}
}
I use Pillarboxes to maintain an AspectRatio of 9/16. So I had to adjust your solution slightly. Instead of calculating the Bound-Width with cameraHeight * screenAspect
I have to make sure it always has the right width: (width - (width x 2 x CameraRect.x )). So the x-Value of my new Vector3 is (cameraHeight * screenAspect) - (cameraHeight * screenAspect * 2f * cam.rect.x);
Answer by JacobWMills · Jun 21, 2014 at 01:47 PM
In case anyone else finds this answer on Google like I did, I wrote a little extension script for the Camera class that adds bounds-like functionality to cameras in general:
public static class CameraExtensions
{
//******Orthographic Camera Only******//
public static Vector2 BoundsMin(this Camera camera)
{
return camera.transform.position.ToVector2() - camera.Extents();
}
public static Vector2 BoundsMax(this Camera camera)
{
return camera.transform.position.ToVector2() + camera.Extents();
}
public static Vector2 Extents(this Camera camera)
{
if (camera.orthographic)
return new Vector2(camera.orthographicSize * Screen.width/Screen.height, camera.orthographicSize);
else
{
Debug.LogError("Camera is not orthographic!", camera);
return new Vector2();
}
}
//*****End of Orthographic Only*****//
}
ToVector2() no longer works in Unity 5. Use the Vector2-cast:
return (Vector2)camera.transform.position - camera.Extents();
Answer by SvGampel · Mar 22, 2015 at 12:16 AM
using UnityEngine;
public static class Extensions
{
public static Bounds OrthographicBounds (this Camera camera)
{
if (!camera.orthographic)
{
Debug.Log(string.Format("The camera {0} is not Orthographic!", camera.name), camera);
return new Bounds();
}
var t = camera.transform;
var x = t.position.x;
var y = t.position.y;
var size = camera.orthographicSize * 2;
var width = size * (float)Screen.width / Screen.height;
var height = size;
return new Bounds(new Vector3(x, y, 0), new Vector3(width, height, 0));
}
}
Answer by Magius96 · Oct 22, 2015 at 08:16 AM
I had a similiar issue lately. My solution was to get the world bounds from the screen, allowing it to adjust to resolution changes. Might not be the best solution, but it works for what I was doing. Note, the code below adds a 5 pixel buffer to the boundary, you can remove that.
private Limits GetLimits()
{
var val=new Limits();
var lowerLeft = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
var upperRight = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
val.Left = lowerLeft.x + 5f;
val.Right = upperRight.x - 5f;
val.Top = upperRight.y - 5f;
val.Bottom = lowerLeft.y + 5f;
return val;
}
public class Limits
{
public float Left { get; set; }
public float Right { get; set; }
public float Top { get; set; }
public float Bottom { get; set; }
}
Your answer
Follow this Question
Related Questions
Resizing orthographic camera to fit 2d sprite on screen 1 Answer
Orthographic Camera settings for a 2D game 1 Answer
Iphone 2d camera settings 1 Answer
2D Camera sizing and movement 0 Answers