I’ll start this Dev blog with a free script I quickly cooked up which some indie devs might find useful to add cinematic effects to their 2D games. The final GameObject will contain a Box Collider 2D trigger which activates the custom script, lerping the main orthographic camera to a desired size over an adjustable Time.deltaTime / smoothing. This lets the Camera size seamlessly change while the player is in game to appropriately accommodate for different level sizes.

Setting up

For this script to work you are going to want to make sure your players GameObject tag is set to “Player” and your Main Camera is named “Main Camera”. Of course you can change the names in the script to suite your liking.

Next create a GameObject and attach the ChangeCamSize.cs script (download from bottom of page). You will need to also attach a Box Collider 2D component and make sure Is Trigger is enabled in the inspector. Resize the Box Collider to your liking and place it anywhere in your scene you want to change camera size when the player collides with the Box Collider.

Script Explanation

I’ll do my best to explain the code, with the script being available for download at the end of the post. Sorry if the code is hard to read on this page, I am yet to find a unity syntax highlighter. Inside the ChangeCamSize script you will notice a few declared variables. The public variables can be conveniently edited outside of MonoDevelop.

// Variables
public float toSize;
public float smoothing;
public float delay = 0;

GameObject gameCamera;
bool triggerActive;
bool playOnce;

toSize is the cameras final lerp size, smoothing adjusts the speed through delta.time and delay  is an additional option to delay the camera lerp.

The OnTriggerEnter2D function will be called when a gameObject with the tag “Player” enters the Boxcolliders area. Its important to make sure your main characters tag is “Player” or change the “Player” tag to your characters own tag. This helps prevent unwanted camera lerping with other objects. The triggerActive bool will insure the camera lerping function is only called once. Once the main character or player collides with the trigger the Coroutine MyDelay is called to activate the triggerActive bool once the delay parameter is met. 

[csharp]

// —- On Trigger Enter —-
void OnTriggerEnter2D(Collider2D other) {
// — Check to see if the collider is the player
if (other.gameObject.tag == "Player" && triggerActive == false) {

// Activate Trigger
StartCoroutine(MyDelay (delay));
}
}

// —- Activate trigger after delay
public IEnumerator MyDelay( float seconds ){
yield return new WaitForSeconds (seconds);
triggerActive = true;
}

[/csharp]

The Start function finds the Main Camera and stores it for later use. The Update function checks to see if triggerActive is true and when it is uses mathf.Lerp to change the Camera to the toSize float over Time.deltaTime / smoothing. Once the Camera is within 0.2 decimal places of the toSize float deactivate the prefab to save memory.

[csharp] // —- Use this for initialization —-
void Start () {
gameCamera = GameObject.Find("Main Camera");
}

// —- Update is called once per frame —-
void Update () {
// — If Trigger is Active & Camera has not already lerped
if (triggerActive){

gameCamera.camera.orthographicSize = Mathf.Lerp(gameCamera.camera.orthographicSize, toSize, Time.deltaTime / smoothing);
float camSize = (Mathf.Round(gameCamera.camera.orthographicSize * 10)/10);
playOnce = (camSize == toSize);

// Deactivate Trigger once camSize reached
if (playOnce) {
gameObject.SetActive(false);
}
}
}[/csharp]

Heres the download link to the script.
And a happy new year to all indies out there, let 2015 be an interesting year!