using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Camera))] public class MultipleTargetCamera : MonoBehaviour { public List targets; public Vector3 offset; public float smoothTime = 0.5f; public float minZoom = 20.0f; public float maxZoom = 7.0f; public float zoomLimiter = 50f; private Vector3 velocity; private Camera cam; private void Start() { cam = GetComponent(); } private void LateUpdate() { if (targets.Count == 0) return; Move(); Zoom(); } void Zoom() { float newZoom = Mathf.Lerp(maxZoom, minZoom, GetGreatestDistance() / zoomLimiter); cam.orthographicSize = newZoom; } float GetGreatestDistance() { var bounds = new Bounds(targets[0].position, Vector3.zero); for (int i = 0; i < targets.Count; i++) { bounds.Encapsulate(targets[i].position); } return bounds.size.x + bounds.size.z / 2; } void Move() { Vector3 centerPoint = GetCenterPoint(); Vector3 newPosition = centerPoint + offset; transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime); } Vector3 GetCenterPoint() { if (targets.Count == 1) { return targets[0].position; } var bounds = new Bounds(targets[0].position, Vector3.zero); for (int i = 0; i < targets.Count; i++) { bounds.Encapsulate(targets[i].position); } return bounds.center; } }