- Home /
Unity 2D Fighting Game Camera
I'm trying to make a 2d fighting camera. I want the camera to focus on 2 fighters and then zoom in/out to make sure they are both on the screen. Using the post below, I was able to get this camera to work horizontally:
https://answers.unity.com/questions/126184/fighting-game-camera.html
The original script is the following
float margin = 1.5f; // space between screen border and nearest fighter
private float z0; // coord z of the fighters plane
private float zCam; // camera distance to the fighters plane
private float wScene; // scene width
private Transform f1; // fighter1 transform
private Transform f2; // fighter2 transform
private float xL; // left screen X coordinate
private float xR; // right screen X coordinate
public void calcScreen(Transform p1, Transform p2)
{
// Calculates the xL and xR screen coordinates
if (p1.position.x < p2.position.x)
{
xL = p1.position.x - margin;
xR = p2.position.x + margin;
}
else
{
xL = p2.position.x - margin;
xR = p1.position.x + margin;
}
}
public void Start()
{
// find references to the fighters
f1 = GameObject.Find("Fighter1").transform;
f2 = GameObject.Find("Fighter2").transform;
// initializes scene size and camera distance
calcScreen(f1, f2);
wScene = xR - xL;
zCam = transform.position.z - z0;
}
public void Update()
{
Vector3 pos = transform.position;
calcScreen(f1, f2);
float width = xR - xL;
if (width > wScene)
{ // if fighters too far adjust camera distance
pos.z = zCam * width / wScene + z0;
}
// centers the camera
pos.x = (xR + xL) / 2;
transform.position = pos;
}
Since on my game you can move horizontally and vertically, I modified the code to also adjust when the character moves vertically to this:
float margin = 1.5f; // space between screen border and nearest fighter
float marginY = 1.5f; // space between screen border and nearest fighter
private float z0; // coord z of the fighters plane
private float zCam; // camera distance to the fighters plane
private float wScene; // scene width
private float hScene; // scene height
private Transform f1; // fighter1 transform
private Transform f2; // fighter2 transform
private float xL; // left screen X coordinate
private float xR; // right screen X coordinate
private float yU; // up screen Y coordinate
private float yD; // down screen Y coordinate
public void calcScreen(Transform p1, Transform p2)
{
// Calculates the xL and xR screen coordinates
if (p1.position.x < p2.position.x)
{
xL = p1.position.x - margin;
xR = p2.position.x + margin;
}
else
{
xL = p2.position.x - margin;
xR = p1.position.x + margin;
}
if (p1.position.y < p2.position.y)
{
yD = p1.position.y - marginY;
yU = p2.position.y + marginY;
}
else
{
yD = p2.position.y - marginY;
yU = p1.position.y + marginY;
}
}
public void Start()
{
// find references to the fighters
f1 = GameObject.Find("Fighter1").transform;
f2 = GameObject.Find("Fighter2").transform;
// initializes scene size and camera distance
calcScreen(f1, f2);
wScene = xR - xL;
hScene = yU - yD;
zCam = transform.position.z - z0;
}
public void Update()
{
Vector3 pos = transform.position;
calcScreen(f1, f2);
float width = xR - xL;
float height = yU - yD;
if (width > wScene)
{ // if fighters too far adjust camera distance
pos.z = zCam * width / wScene + z0;
}
else if (height > hScene)
{ // if fighters too far adjust camera distance
pos.z = zCam * height / hScene + z0;
}
// centers the camera
pos.x = (xR + xL) / 2;
pos.y = (yU + yD) / 2;
transform.position = pos;
}
This works when the characters only moves horizontally or vertically, if it moves both horizontally and vertically it doesnt work anymore. Also, the vertical movement doesnt work appropiately when the 2 fighters are far way. The camera zooms in even thought they are far from each other but they are close in therms of the height.
I feel like I need to change the code to look at both x and y at the same time. It feels like I'm considering and x separately.
I have been stuck on this for a while now. Any help would be appreciated.
Your answer
Follow this Question
Related Questions
Follow camera in 2d game 2 Answers
Cinemachine Confiner 2D shift my camera view 0 Answers
Keeping UI Element To Game Object When Camera Moves 2 Answers
Smooth camera script looking at left hand side of character. 0 Answers
My game is laggy, but there are only a ball and some flat platforms. 3 Answers