2D Camera to follow two players

How could I go about making a 2D camera like this. Any help is appreciated.

I shouldn’t answer this question, because I don’t really see any effort and research in your post (you’re basically asking for code), but whatever, this is quite an interesting problem and the solution might be useful for other people as well.

It’s pretty simple, the only things you have to know are Camera.ortographicSize, Screen.width and Screen.height.

You need to create a script that computes the middle point between both players and sets it to the camera’s position:

void SetCameraPos() {
	Vector3 middle = (player1.position + player2.position) * 0.5f;

	camera.transform.position = new Vector3(
		middle.x,
		middle.y,
		camera.transform.position.z
	);
}

If you also want to dynamically change the size of the camera:

void SetCameraSize() {
	//horizontal size is based on actual screen ratio
	float minSizeX = minSizeY * Screen.width / Screen.height;

	//multiplying by 0.5, because the ortographicSize is actually half the height
	float width = Mathf.Abs(player1.position.x - player2.position.x) * 0.5f;
	float height = Mathf.Abs(player1.position.y - player2.position.y) * 0.5f;

	//computing the size
	float camSizeX = Mathf.Max(width, minSizeX);
	camera.orthographicSize = Mathf.Max(height,
		camSizeX * Screen.height / Screen.width, minSizeY);
}

The minSizeX and minSizeY variables are just a low limit of the camera’s size, i.e., the camera won’t be smaller than [minSizeX, minSizeY].

In Update function you just call both functions:

void Update() {
    SetCameraPos();
    SetCameraSize();
}

So, the code is in fact really simple. I wanted to keep it clean by separating both functions. It does only the basics though. You’ll likely need to add some margin and other features like smooth camera transition, but I leave that kind of stuff for your own research.

Here’s the full basic script, enjoy:

using UnityEngine;
using System.Collections;

public class TestCam : MonoBehaviour {
	public Transform player1, player2;
	public float minSizeY = 5f;

	void SetCameraPos() {
		Vector3 middle = (player1.position + player2.position) * 0.5f;

		camera.transform.position = new Vector3(
			middle.x,
			middle.y,
			camera.transform.position.z
		);
	}

	void SetCameraSize() {
		//horizontal size is based on actual screen ratio
		float minSizeX = minSizeY * Screen.width / Screen.height;

		//multiplying by 0.5, because the ortographicSize is actually half the height
		float width = Mathf.Abs(player1.position.x - player2.position.x) * 0.5f;
		float height = Mathf.Abs(player1.position.y - player2.position.y) * 0.5f;

		//computing the size
		float camSizeX = Mathf.Max(width, minSizeX);
		camera.orthographicSize = Mathf.Max(height,
			camSizeX * Screen.height / Screen.width, minSizeY);
	}

	void Update() {
		SetCameraPos();
		SetCameraSize();
	}
}

Search UA/Google on multiple cameras for other examples.

It’s okay I solved it myself.

In Unity 5 I found that if you let it update the script (which it will do automatically after going back to the editor) If you add in

public float orthographicSize;

Then set the number in the editor the script will work.

pliz gys i want use this script in my game hellp