Centre Perspective view camera to 4 players

Hi there, I have a script that allows me to keep a camera between 2 players and always keeping them in the view of the screen but I’m looking to do this same system with 4 players.

I have tried a view different tactics but nothing seems to be working? I Have also tried google and nothing came of that has been any use to me.

Would someone perhaps be able to solve my issue? The script will be below.
Thanks in Advance.

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

public class CameraControl : MonoBehaviour
{

    public Transform m_Player01;
    public Transform m_Player02;
    public Transform m_Player03;
    public Transform m_Player04;

    private const float DISTANCE_MARGIN = 1.0f;

    private Vector3 _MiddlePoint;
    private float _DistanceFromMiddlePoint;
    private float _DistanceBetweenPlayers;
    private float _CameraDistance;
    private float _AspectRatio;
    private float _Fov;
    private float _TanFov;

    void Start()
    {
        _AspectRatio = Screen.width / Screen.height;
        _TanFov = Mathf.Tan(Mathf.Deg2Rad * Camera.main.fieldOfView / 2.0f);
    }

    void Update()
    {
        // Position the camera in the center.
        Vector3 newCameraPos = Camera.main.transform.position;
        newCameraPos.x = _MiddlePoint.x + 10f;
        newCameraPos.y = _MiddlePoint.y;
        newCameraPos.z = _MiddlePoint.z;
        Camera.main.transform.position = newCameraPos;

        // Find the middle point between players.
        Vector3 vectorBetweenPlayers = m_Player02.position - m_Player01.position;
        _MiddlePoint = m_Player01.position + 0.5f * vectorBetweenPlayers;

        // Calculate the new distance.
        _DistanceBetweenPlayers = vectorBetweenPlayers.magnitude;
        _CameraDistance = (_DistanceBetweenPlayers / 2.0f / _AspectRatio) / _TanFov;

        // Set camera to new position.
        Vector3 dir = (Camera.main.transform.position - _MiddlePoint).normalized;
        Camera.main.transform.position = _MiddlePoint + dir * (_CameraDistance + DISTANCE_MARGIN);
    }
}

Here’s a tidy way to do it.

        public virtual void FollowTargets()
        {
            Vector3 averagePos = GetAveragePos(Targets);
            transform.position = averagePos + Offset;
        }
        protected virtual Vector3 GetAveragePos(List<Transform> targets)
        {
            Vector3 averagePos = Vector3.zero;
            Vector3[] positions = new Vector3[targets.Count];
            for (int i = 0; i < targets.Count; i++)
            {
                positions[i] = targets[i].transform.position;
                averagePos += positions[i];
            }
            return averagePos / positions.Length;
        }

You can just make Offset some physical offset from the target average position. This was done for TopDown type games.

Where in this shorten version should I place the camera movement or what would be the best way of writing it?
Appreciate the help btw!

It’s being set in the FollowTargets() method.

Ah yes sorry, im trying to get it change the zoom too when the objects go out of view of the camera.

You would probably have to measure the distances between targets and map / clamp that to a multiplier for the Offset parameter. In my case I had setup some collider boundaries at the camera frustum so there was no need to zoom.

1 Like

That is massive help to get it to follow all 4 players, just wondering would there be a way of getting it to keep them all in the view? So the further the players gets from each other the more the camera zooms out to keep the all in view and if they get close together the camera zooms in closer.