# Smooth follow camera keeping track of multiple target

Hi

I am having a problem. I would like to make the smooth follow camera have multiple targets and zoom in and out to keep track of its targets.

There will be no switching of targets, all targets should be in view at all time

I am having problems coming up with the logic for doing this. How could I go about doing this?

Any help is appreciated.

Thanks,

hmm, never did thisâ€¦ but i would start this way:

-given that your environment is top-down and your camera is unrotated ( x is left&right on the viewport, z is top and bottom)
-get the the lowest and highest position.x value among the objects
-get the the lowest and highest position.z value among the objects
-the middle between those positions is your x and z value where your camera should be centered at
-then calculate the distance to ground (zoom) of your camera:
â€“based on the fov your distance would be equal to (zCenter-zMax) of the objects if your fov is 90
â€“use z because fov is the vertical camera angle
â€“if your fov is other than 90 you have to compensate for that, if its only 45 you would have to use 2*zCenter-zMax as camera height
â€“be sure to clamp the minimum camera height at a reasonable level

picture that could help

``````//the formulas after you found the min and max values:
float xCenter = (xMax-xMin)/2F;
float zCenter = (zMax-zMin)/2F;

float height = 90F/Camera.fieldOfView * zMax-zCenter * 1.2F
//*1.2 to zoom out a bit farther then needed, the objects would be on the very edge if not

Vector3 unSmoothCameraPosition = new Vector3(xCenter, height, zCenter);
cameraTransform.position = Vector3.Lerp(cameraTransform.position,unSmoothCameraPosition,Time.deltaTime*gm.cameraSmoothFactor);
``````

if you are not top down however, forget everything i said
if your camera is rotated use the a rotated localposition of an object with the same rotation as the camera or sin() cos() to calculate the referential to the camera

1 Like

A minor correction here, if someone else is interested to implement it on their project.
You will have to change the way xCenter and zCenter are calculated, because right now itâ€™s not the real center, you should do something like this:

``````float xCenter = xMin + (Mathf.Abs(xMax-xMin)/2F);
float zCenter = zMin + (Mathf.Abs(zMax-zMin)/2F);
``````

Finally I added an offset on unSmoothCameraPosition to balance the fact that my camera was angled by 70 degrees.

Cheers !

1 Like

Thanks for correcting it, i actually never tested the code and wrote the whole comment in the office, also: MSpaint

Subaeria looks stunning, did you apply the code to it?

Thanks but no, actually I participated to the Global game jam this weekend, we made this game:
http://globalgamejam.org/2016/games/idle-idol
You can try it, you will see the camera moving like what you said

On Subaeria we use a far more complicated camera system :-/

thatâ€™s a cool tutorial indeed but I think this is for orthographic cameras, which is quite different.

Hi there, I managed to achieve it by using the script on the tanks tutorial and modifying the zoom method to match thisâ€¦

``````  private void Zoom()
{
// Find the required size based on the desired position and smoothly transition to that size.
float requiredSize = FindRequiredSize();
if (m_Camera.orthographic)
{
m_Camera.orthographicSize = Mathf.SmoothDamp(m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
}
else
{
Vector3 newPos = transform.position;
newPos.y = (90f / m_Camera.fieldOfView) * requiredSize;
newPos.y = Mathf.SmoothDamp(transform.position.y, newPos.y, ref m_ZoomSpeed, m_DampTime);
transform.position = newPos;
}
}
``````

I must say my camera is looking straight to the floor, further calculations should be used if the camera has any other rotation