I ahve already kind of made the camera script but i actually just wanted help with setting a min Distance so that it doesnt zoom in too much if they are right next to each other
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class twoPlayerCamera : MonoBehaviour
{
public GameObject Tim;
public GameObject Tom;
private Camera cam;
private void Start()
{
cam = Camera.main;
}
private void Update()
{
float zoomFactor = 1.5f;
float followTimeDelta = 0.8f;
Vector3 midPoint = (Tim.transform.position + Tom.transform.position) / 2f;
float distance = (Tim.transform.position - Tom.transform.position).magnitude;
Vector3 cameraDestination = midPoint - cam.transform.forward * distance * zoomFactor;
if (cam.orthographic)
{
cam.orthographicSize = distance;
}
cam.transform.position = Vector3.Slerp(cam.transform.position, cameraDestination, followTimeDelta);
if ((cameraDestination - cam.transform.position).magnitude <= 0.05f)
{
cam.transform.position = cameraDestination;
}
}
}
Here’s something I put together for a local multiplayer game that had up to 4 players and can support 2+ players.
using System.Collections.Generic;
using UnityEngine;
public class TrackingCamera : MonoBehaviour
{
[SerializeField]
private List<Transform> _targets = new List<Transform>();
[SerializeField]
private float _boundingBoxPadding = 2f;
[SerializeField]
private float _minimumOrthographicSize = 8f;
[SerializeField]
private float _zoomSpeed = 20f;
private Camera _camera;
public void Awake()
{
_camera = GetComponent<Camera>();
_camera.orthographic = true;
}
public void AddTarget(Transform target)
{
if (_targets.Contains(target))
return;
_targets.Add(target);
}
public void RemoveTarget(Transform target)
{
if (_targets.Contains(target))
{
_targets.Remove(target);
}
}
public void LateUpdate()
{
if (_targets == null || _targets.Count < 1)
return;
var boundingBox = CalculateTargetsBoundingBox();
transform.position = CalculateCameraPosition(boundingBox);
_camera.orthographicSize = CalculateOrthographicSize(boundingBox);
}
/// <summary>
/// Calculates a bounding box that contains all the targets.
/// </summary>
/// <returns>A Rect containing all the targets.</returns>
private Rect CalculateTargetsBoundingBox()
{
var minX = Mathf.Infinity;
var maxX = Mathf.NegativeInfinity;
var minY = Mathf.Infinity;
var maxY = Mathf.NegativeInfinity;
foreach (var target in _targets)
{
var position = target.position;
minX = Mathf.Min(minX, position.x);
minY = Mathf.Min(minY, position.y);
maxX = Mathf.Max(maxX, position.x);
maxY = Mathf.Max(maxY, position.y);
}
return Rect.MinMaxRect(minX - _boundingBoxPadding, maxY + _boundingBoxPadding, maxX + _boundingBoxPadding, minY - _boundingBoxPadding);
}
/// <summary>
/// Calculates a camera position given the a bounding box containing all the targets.
/// </summary>
/// <param name="boundingBox">A Rect bounding box containg all targets.</param>
/// <returns>A Vector3 in the center of the bounding box.</returns>
private Vector3 CalculateCameraPosition(Rect boundingBox)
{
var boundingBoxCenter = boundingBox.center;
return new Vector3(boundingBoxCenter.x, boundingBoxCenter.y, _camera.transform.position.z);
}
/// <summary>
/// Calculates a new orthographic size for the camera based on the target bounding box.
/// </summary>
/// <param name="boundingBox">A Rect bounding box containg all targets.</param>
/// <returns>A float for the orthographic size.</returns>
private float CalculateOrthographicSize(Rect boundingBox)
{
float orthographicSize;
var topRight = new Vector3(boundingBox.x + boundingBox.width, boundingBox.y, 0f);
var topRightAsViewport = _camera.WorldToViewportPoint(topRight);
if (topRightAsViewport.x >= topRightAsViewport.y)
orthographicSize = Mathf.Abs(boundingBox.width) / _camera.aspect / 2f;
else
orthographicSize = Mathf.Abs(boundingBox.height) / 2f;
return Mathf.Clamp(Mathf.Lerp(_camera.orthographicSize, orthographicSize, Time.deltaTime * _zoomSpeed), _minimumOrthographicSize, Mathf.Infinity);
}
}