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?

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;
    }

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;
    }
}

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*****//
}

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; }
}

Hi everyone
my solution is :

public class Utility
{
	public static Bounds OrthographicBounds( Camera camera)
	{
		float cameraHeight = camera.orthographicSize * 2;
		Bounds bounds = new Bounds(
			camera.transform.position,
			new Vector3(cameraHeight * camera.aspect, cameraHeight, 0));
		return bounds;
	}
}

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));
	}
}

This is what worked for me to get the limits for the camera position:

float camExtentV = _cam.orthographicSize;  
float camExtentH = (camExtentV * Screen.width) / Screen.height;

Bounds levelBounds = LevelBoundsSpriteRenderer.bounds;

float leftBound = levelBounds.min.x + camExtentH;
float rightBound = levelBounds.max.x - camExtentH;
float bottomBound = levelBounds.min.y + camExtentV;
float topBound = levelBounds.max.y - camExtentV;

Since this will give different results in “game screen” and “editor screen” I took @SvGampel 's answer and modified it a little:

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /********************
     * CAMERA  *
     ********************/
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Returns the orthographic camera bounds
    /// </summary>
    /// <param name="_Camera"></param>
    /// <returns></returns>
    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();
        }

        Transform camTrans = _Camera.transform;
        float scaleWidthFactor = (float)Screen.currentResolution.width / (float)Screen.currentResolution.height;
        float height = _Camera.orthographicSize * 2f;
        float width = height * scaleWidthFactor;
        float viewDepth = _Camera.farClipPlane - _Camera.nearClipPlane;

        return new Bounds(new Vector3(camTrans.position.x, camTrans.position.y, camTrans.position.z), new Vector3(width, height, viewDepth));
    }

    /// <summary>
    /// Returns the orthographic camera rect (without z)
    /// </summary>
    /// <param name="_Camera"></param>
    /// <returns></returns>
    public static Rect OrthographicRect(this Camera _Camera)
    {
        if (!_Camera.orthographic)
        {
            Debug.Log(string.Format("The camera {0} is not Orthographic!", _Camera.name), _Camera);
            return new Rect();
        }

        Transform camTrans = _Camera.transform;
        float scaleWidthFactor = (float)Screen.currentResolution.width / (float)Screen.currentResolution.height;
        float height = _Camera.orthographicSize * 2f;
        float width = height * scaleWidthFactor;

        return new Rect(new Vector2(camTrans.position.x - (width*0.5f), camTrans.position.y - (height * 0.5f)), new Vector2(width, height));
    }

This code will always give the set resolution. (float)Screen.currentResolution.width instead of (float)Screen.width .