This site uses strictly necessary cookies. More Information

X- Home /

**Question**by cornell-lindsay · Mar 01, 2015 at 09:58 AM · cameracamera-movementcamera followcamera movement

# Make a camera to Follow two players

so in my game i have a local co-op and i want a single camera to follow the players. this camera should be at a specified distance from the players but zoom in or zoom out, smoothly based on the distance between the two players like most old coop games. i have attempted to make it but it does not really work. can anyone suggest how to fix what i have or a whole new way of doing it. any help appreciated.

public class CameraFollow : MonoBehaviour {

```
public Vector3 distance;
public Transform target1;
public Transform target2;
public float CamZPosition;
public float TargetZmiddle;
public float camDistance;
public float trial;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
distance = target1.position - target2.position;
TargetZmiddle = ((target1.position.z * target2.position.z) /2);
if(distance.x < 0)
distance.x = distance.x * -1;
if(distance.z < 0)
distance.z = distance.z * -1;
trial = camDistance + distance.x;
if(distance.z > 20)
{
//camDistance = trial;
}
if(distance.x > 20)
{
camDistance = trial;
}
if(distance.x < 19 || distance.z < 19)
{
camDistance = 10;
}
//Debug.Log (TargetZmiddle);
transform.position = new Vector3(transform.position.x, transform.position.y, (TargetZmiddle - camDistance));
}
```

}

Point the camera halfway between the players. (I think you've done this)

$$anonymous$$ight be simple to use 'isVisible' to determine when the camera stops zooming.

Really though, you will want to work out the frustum based on distances which can be worked out on paper with a few triangles.

You're method is probably getting there but a bit of trial and error is going to be the key.

distance and camDistance are not related. What are they intended to do?

pos 1 and pos2 are both set from camera position and both feed back in to camera position one after the other.

Your right CamDistance and distance are not related in any way distance is a the distance between the two players as a vector 3

CamDistance is the value in the z axis that the camera will be 'zoomed' from the halfway point of the players.

Pos is a workaround as I couldn't get math clamp working so once the players are near the camera edge they cannot move any further from the camera view

camDistance doesn't seem to get updated.

You could use this to move the camera backwards to extend the view.

Thats what CamOffset does, CamDistance stays at 10 to make sure the camera stays at least 10 units away from the midpoint as without it when the players got close to each other the camera would zoom in soo much that they would take up the whole screen

**Answer** by cornell-lindsay
·
Mar 01, 2015 at 01:18 AM

for anyone that wants to know i have completed this function the final code is below

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class CameraFollow : MonoBehaviour { public static CameraFollow cFollow;

```
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform target;
public float MidX;
public float MidY;
public float MidZ;
public Transform target1;
public Transform target2;
public Vector3 Midpoint;
public Vector3 distance;
public float camDistance;
public float CamOffset;
public float bounds;
void Awake ()
{
//checking if there's already a game manager
if(cFollow == null)
{
DontDestroyOnLoad (gameObject);
cFollow = this;
}
else if(cFollow != this)
{
Destroy(gameObject);
}
}
// Use this for initialization
void Start ()
{
camDistance = 10.0f;
bounds = 12.0f;
}
// Update is called once per frame
void Update ()
{
distance = target1.position - target2.position;
if(camDistance >= 19.0f)
camDistance = 19.0f;
if (camDistance <= 10.0f)
camDistance = 10.0f;
if(distance.x < 0)
distance.x = distance.x * -1;
if(distance.z < 0)
distance.z = distance.z * -1;
if(target1.position.x < (transform.position.x -bounds))
{
Vector3 pos = target1.position;
pos.x = transform.position.x -bounds;
target1.position = pos;
}
if(target2.position.x < (transform.position.x -bounds))
{
Vector3 pos = target2.position;
pos.x = transform.position.x -bounds;
target2.position = pos;
}
if(target1.position.x > (transform.position.x +bounds))
{
Vector3 pos = target1.position;
pos.x = transform.position.x +bounds;
target1.position = pos;
}
if(target2.position.x > (transform.position.x +bounds))
{
Vector3 pos = target2.position;
pos.x = transform.position.x +bounds;
target2.position = pos;
}
if(distance.x > 15.0f)
{
CamOffset = distance.x * 0.3f;
if(CamOffset >=8.5f)
CamOffset = 8.5f;
}else if(distance.x < 14.0f)
{
CamOffset = distance.x * 0.3f;
}else if( distance.z < 14.0f)
{
CamOffset = distance.x * 0.3f;
}
MidX = (target2.position.x + target1.position.x) /2;
MidY = (target2.position.y + target1.position.y) /2;
MidZ = (target2.position.z + target1.position.z) /2;
Midpoint = new Vector3 (MidX, MidY, MidZ);
if (target1)
{
Vector3 point = camera.WorldToViewportPoint(Midpoint);
Vector3 delta = Midpoint - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, camDistance + CamOffset)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
```

this was the code used pretty sure its exactly the same as previous poster

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class CameraFollow : MonoBehaviour { public static CameraFollow cFollow;

```
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform target;
public float MidX;
public float MidY;
public float MidZ;
public Transform target1;
public Transform target2;
public Vector3 Midpoint;
public Vector3 distance;
public float camDistance;
public float CamOffset;
public float bounds;
// Use this for initialization
void Start ()
{
camDistance = 10.0f;
bounds = 12.0f;
}
// Update is called once per frame
void Update ()
{
distance = target1.position - target2.position;
if(camDistance >= 19.0f)
camDistance = 19.0f;
if (camDistance <= 10.0f)
camDistance = 10.0f;
if(distance.x < 0)
distance.x = distance.x * -1;
if(distance.z < 0)
distance.z = distance.z * -1;
if(target1.position.x < (transform.position.x -bounds))
{
Vector3 pos = target1.position;
pos.x = transform.position.x -bounds;
target1.position = pos;
}
if(target2.position.x < (transform.position.x -bounds))
{
Vector3 pos = target2.position;
pos.x = transform.position.x -bounds;
target2.position = pos;
}
if(target1.position.x > (transform.position.x +bounds))
{
Vector3 pos = target1.position;
pos.x = transform.position.x +bounds;
target1.position = pos;
}
if(target2.position.x > (transform.position.x +bounds))
{
Vector3 pos = target2.position;
pos.x = transform.position.x +bounds;
target2.position = pos;
}
if(distance.x > 15.0f)
{
CamOffset = distance.x * 0.3f;
if(CamOffset >=8.5f)
CamOffset = 8.5f;
}else if(distance.x < 14.0f)
{
CamOffset = distance.x * 0.3f;
}else if( distance.z < 14.0f)
{
CamOffset = distance.x * 0.3f;
}
MidX = (target2.position.x + target1.position.x) /2;
MidY = (target2.position.y + target1.position.y) /2;
MidZ = (target2.position.z + target1.position.z) /2;
Midpoint = new Vector3 (MidX, MidY, MidZ);
Vector3 delta = Midpoint - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, camDistance + CamOffset));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
```

}

if that doesnt work drop your email and i'll send you the whole unity file and you can reverse engineer it from that

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Help make camera zoom smoother 3 Answers

Smooth camera on moving platform 1 Answer

How to make a camera Follow an Object moving in zigzag path? 1 Answer

camera follow gameobject moving in angular path. 0 Answers

Making camera follow upwards 2 Answers