# Keep Player + Party and/or Targeted Enemies in Camera Focus

I was planning on making a multiplayer game that local multiplayer, but decided that I would have the game on one computer.

My camera currently allows 4 four players to be on a screen at once. The game I intend on creating will allow up 2-4 for players and I'm thinking that want to limit the distance they can move based on a zoom out limit variable or make them teleport behind the 1st player if they get to far from group.

The problem is I can't figure out how to zoom in and out when the players get for or closer. I also want it to become a little bit "top-down" as the players get farther away from each other and more third person when the get closer.

This might help you get the idea (even though the game is 2D) I want the camera to zoom in-out based on the player's distance from each other. I would also like it not to just be limited to 4 players, but for 2-3 players also.

Here is the camera script I have so far.

``````//========================================
//Currently, there are for slots to put players in for camera focus
var lookAtTarget1 : Transform;
var lookAtTarget2 : Transform;
var lookAtTarget3 : Transform;
var lookAtTarget4 : Transform;

//Place the camera under this in the inspector
var theCamera : Transform;

//How high the camera is currently (should be replaced later for zom variables)
var height : float = 10.0;

//How close the camera is to the average spot that it is focused at.
var distance = 12.0;

//Temp Zoom Variable
var zoom = 10.0;

//This checks the average location of the players in coordinates.
var avgDistance;
//========================================

function LateUpdate ()

{
//If the first target doesn't exist, exit this statement
if (!lookAtTarget1)
return;

//Check for the average distance between the four players.
avgDistance = ((lookAtTarget1.position+lookAtTarget2.position+lookAtTarget3.position+lookAtTarget4.position)/4);
Debug.Log(avgDistance);

theCamera.transform.position.x = avgDistance.x ;
theCamera.transform.position.z = avgDistance.z - distance;
theCamera.transform.position.y = avgDistance.y + height;

//Set height and distance variable to zoom variable
height = zoom;
distance = zoom;
}

``````

This script takes the largest difference between all object positions and lerps the camera with that value. The larger the difference between objects, the greater the camera distance.

To test, assign 4 cubes to the script, and move them in the editor after hitting play. The camera will move according to the greatest overall distance.

*EDIT this script will search for any gameobjects with the tag "Player" and assign those to the targets array. This happens every update so if a target dies, it is removed from the target array immediately. Also, it is not limited to 4, you can use as many targets as you like, however only targets with the "Player" tag will be included.

*EDIT 2 upated to fix a small bug. 12:26

*EDIT 3 updated to fix player being clipped. 11:21 -- 1/31

*EDIT 4 updated to allow Orthographic Cameras (it will auto detect Orthographic camera) 10:28 -- 2/26/12

``````
////////////////////////////////////////////////////////////////////

//IMPORTANT! Tag ALL players with "Player" so they are recognized.//

////////////////////////////////////////////////////////////////////

private var isOrthographic : boolean;
var targets : GameObject[];
var currentDistance : float;
var largestDistance : float;
var theCamera : Camera;
var height : float = 5.0;
var avgDistance;
var distance = 0.0;                    // Default Distance
var speed = 1;
var offset : float;

//========================================

function Start(){

targets = GameObject.FindGameObjectsWithTag("Player");

if(theCamera) isOrthographic = theCamera.orthographic;

}

function OnGUI(){

GUILayout.Label("largest distance is = " + largestDistance.ToString());

GUILayout.Label("height = " + height.ToString());

GUILayout.Label("number of players = " + targets.length.ToString());

}

function LateUpdate ()

{

targets = GameObject.FindGameObjectsWithTag("Player");

if (!GameObject.FindWithTag("Player"))

return;

var sum = Vector3(0,0,0);

for (var n = 0; n < targets.length ; n++){

sum += targets[n].transform.position;

}

var avgDistance = sum / targets.length;

//    Debug.Log(avgDistance);

var largestDifference = returnLargestDifference();

height = Mathf.Lerp(height,largestDifference,Time.deltaTime * speed);

if(isOrthographic){

theCamera.transform.position.x = avgDistance.x ;

theCamera.orthographicSize = largestDifference;

theCamera.transform.position.y = height;

theCamera.transform.LookAt(avgDistance);

} else {

theCamera.transform.position.x = avgDistance.x ;

theCamera.transform.position.z = avgDistance.z - distance + largestDifference;

theCamera.transform.position.y = height;

theCamera.transform.LookAt(avgDistance);

}

}

function returnLargestDifference(){

currentDistance = 0.0;

largestDistance = 0.0;

for(var i = 0; i < targets.length; i++){

for(var j = 0; j <  targets.length; j++){

currentDistance = Vector3.Distance(targets*.transform.position,targets[j].transform.position);*
*if(currentDistance > largestDistance){*
*largestDistance = currentDistance;*
*}*
*}*
*}*
*return largestDistance;*
*}*
*```*``````