Bounded camera 2D

This is just a simple script to bound the camera within a 2D rectangle. The player should be able to move with the camera following him. As the player moves around, the camera should also zoom in (idle) and out (moving) so that it shows more of the world while walking around.

The script is using the input to make it easy to port to a project and start using it.

Basic requirements

The camera should move up and down without showing the outer of the level (most likely the blue background of the camera by default). So the player should be able to get to the edge but the camera would stay centered.

As we mentioned, the camera should zoom in and out while moving to display a larger part of the level.

In order to start testing, place a cube on a level, then add two empty game objects. One goes at bottom left, other goes at top right. They define the min and max position of the camera.

The script

public class Test : MonoBehaviour {

    public float speed = 2f;
    public Transform min,max;
    private float aspect;
    private float size = 1.5f;
    private float largeSize = 2f;
    private void Start () 
        this.aspect = Camera.main.aspect;

    private void Update () 
        float x = Input.GetAxis ("Horizontal");
        float y = Input.GetAxis ("Vertical");
        if (x != 0f || y != 0f) {
            Camera.main.orthographicSize = Mathf.MoveTowards (Camera.main.orthographicSize, largeSize,  Time.deltaTime);
        } else {
            Camera.main.orthographicSize = Mathf.MoveTowards (Camera.main.orthographicSize, size, Time.deltaTime);
        Vector3 position = this.transform.position;
        position.x += x * Time.deltaTime * this.speed;
        position.y += y * Time.deltaTime * this.speed;
        float orthSize = Camera.main.orthographicSize;

        if (position.x < (this.min.position.x + orthSize * this.aspect)) 
            position.x = this.min.position.x + orthSize * this.aspect;
        else if (position.x > (this.max.position.x - orthSize * this.aspect)) 
            position.x = this.max.position.x - orthSize * this.aspect;
        if (position.y < (this.min.position.y + orthSize))
            position.y = this.min.position.y + orthSize;
        else if(position.y > (this.max.position.y- orthSize)) 
             position.y = this.max.position.y- orthSize;
        this.transform.position = position;

This is fairly simple, the camera size listens to the x and y movements. If none, it lerps to idle size, if one axis is used, it lerps to large size.

The x and y positions are constrained relative to the current orthographic size. Since the size is half of the vertical camera view, we need to use the aspect ratio to get the size of the horizontal view. This is used for the x position.

This is pretty much it for this script. It is fairly simple and should be used to start a larger movement system. Also, the movement should come from the player movement. A simple way would be to either attach to the player or add a public method that sets the x and y from the player movement script.