Dynamic Orthagraphic Camera Zoom

So im working on a 2d 2player student project, we are trying to have a camera that tracks both players.
Atm ive worked out how to keep the cam in the middle of the players but as for the cam distance its been a little tricky trying to work it out.
the code isent the most elegant so far but it does the job

var player1 : Transform;
var player2 : Transform;

function Update ()
{
var sum1 : float;
var sum2 : float;
var sum3 : float;
var sum4 : float;

sum1 = player1.position.x + player2.position.x;
sum2 = sum1 / 2;
transform.position.x = sum2;

sum3 = player1.position.z + player2.position.z;
sum4 = sum3 / 2;
transform.position.z = sum4;

}

If someone could point me in the right direction it would be much appreciated

One of the ways you can use to do that can be using the distance between both players to calculate the camera’s y position.

I just added the following to your code:

transform.position.y = Vector3.Distance(player1.position, player2.position) * 0.75;

and it looked like it may be what you need (the “* 0.75” is a value that you’ll probably wanna tweak to your project).

Hope it helps.

EDIT: Sorry, didn’t notice you have an orthographic camera, but the same applies: you just map the value to the orthographicSize parameter.

camera.orthographicSize = Vector3.Distance(player1.position, player2.position) * 0.75;