CameraController.cs (3688B)
1 using System.Collections.Generic; 2 using UnityEngine; 3 public class CameraController : MonoBehaviour { 4 5 public float dampTime = 0.2f; 6 public float zoomLimit = 50f; 7 public float minZoom = 40f; 8 public float maxZoom = 10f; 9 10 [SerializeField] private bool newCamera; 11 12 public float maxDistanceFromOtherPlayers = 70; 13 [HideInInspector] public List<Transform> players = new List<Transform>(); 14 //private List<Transform> unSeenPlayers = new List<Transform>(); 15 //private List<float> localDistances = new List<float>(); 16 17 private UnityEngine.Camera cCamera; 18 private Vector3 cameraVelocity; 19 20 SpawnManagerV2 spawnManager; 21 22 //public bool CameraOn; 23 24 private int numberPlayers; 25 26 public Vector3 offset; 27 private void Awake() 28 { 29 if (!newCamera) 30 { 31 Debug.Log("CameraStart!!!"); 32 spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManagerV2>(); 33 numberPlayers = PublicStatHandler.GetInstance().amountOfPlayers; 34 } 35 36 cCamera = GetComponentInChildren<UnityEngine.Camera>(); 37 38 for (int i = 0; i < numberPlayers; i++) 39 { 40 Debug.Log(spawnManager.spawnedPlayers[i].transform); 41 players.Add(spawnManager.spawnedPlayers[i].gameObject.transform); 42 } 43 } 44 45 private void FixedUpdate() 46 { 47 /*foreach (Transform slimesTransform in players) 48 { 49 foreach (Transform slimesTransform2 in players) 50 { 51 float localdist = Vector3.Distance(slimesTransform.position, slimesTransform2.position); 52 localDistances.Add(localdist); 53 } 54 55 localDistances.Sort(); 56 //Debug.Log(localDistances[players.Count - 1]); 57 distances.Add(localDistances[players.Count - 1]); 58 localDistances.Clear(); 59 } 60 61 distances.Sort(); 62 if (distances[distances.Count - 1] > maxDistanceFromOtherPlayers) 63 { 64 Debug.Log("OutOfBounds"); 65 } 66 distances.Clear();*/ 67 68 Vector3 centerPoint = GetCenterPoint(); 69 Vector3 desPos = centerPoint + offset; 70 transform.position = Vector3.SmoothDamp(transform.position, desPos, ref cameraVelocity, dampTime); 71 72 float theZoom = Mathf.Lerp(maxZoom, minZoom, FindSize() / zoomLimit); 73 cCamera.fieldOfView = Mathf.Lerp(cCamera.fieldOfView, theZoom, Time.deltaTime); 74 } 75 76 private Vector3 GetCenterPoint() 77 { 78 switch (players.Count) 79 { 80 case 0: 81 return Vector3.zero; 82 case 1: 83 return players[0].position; 84 default: 85 return FindBoundsAndSetBounds().center; 86 } 87 } 88 89 private float FindSize() 90 { 91 return FindBoundsAndSetBounds().size.x; 92 } 93 94 private Bounds FindBoundsAndSetBounds() 95 { 96 if (players.Count == 0) 97 { 98 Bounds zeroBounds = new Bounds(Vector3.zero, Vector3.zero); 99 return zeroBounds; 100 101 } 102 Bounds playerBounds = new Bounds(players[0].position, Vector3.zero); 103 foreach (var slimes in players) 104 { 105 playerBounds.Encapsulate(slimes.position); 106 } 107 return playerBounds; 108 } 109 110 public void AddPlayerToPlayerToCameraList(GameObject player) 111 { 112 players.Add(player.transform); 113 } 114 }