slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

CameraControllerNew.cs (2636B)


      1 using System.Collections.Generic;
      2 using UnityEngine;
      3 
      4 namespace Slimecing.Camera
      5 {
      6     public class CameraControllerNew : MonoBehaviour {
      7 
      8         public float dampTime = 0.2f;
      9         public float zoomLimit = 50f;
     10         public float minZoom = 40f;
     11         public float maxZoom = 10f;
     12 
     13         public float maxDistanceFromOtherPlayers = 70;
     14 
     15         private List<Transform> visibleThings = new List<Transform>();
     16         //private List<Transform> unSeenPlayers = new List<Transform>();
     17         //private List<float> localDistances = new List<float>();
     18         private UnityEngine.Camera _cCamera;                
     19         private Vector3 _cameraVelocity;
     20         private int _numberPlayers;
     21         public Vector3 offset;
     22 
     23         public List<Transform> VisibleThings { get => visibleThings; set => visibleThings = value; }
     24         
     25         private void Awake()
     26         {
     27             _cCamera = GetComponent<UnityEngine.Camera>();
     28         }
     29 
     30         private void FixedUpdate()
     31         {
     32             Vector3 centerPoint = GetCenterPoint();
     33             Vector3 desPos = centerPoint + offset;
     34             transform.position = Vector3.SmoothDamp(transform.position, desPos, ref _cameraVelocity, dampTime);
     35 
     36             float theZoom = Mathf.Lerp(maxZoom, minZoom, FindSize() / zoomLimit);
     37             _cCamera.fieldOfView = Mathf.Lerp(_cCamera.fieldOfView, theZoom, Time.deltaTime);
     38         }
     39 
     40         private Vector3 GetCenterPoint()
     41         {
     42             switch (VisibleThings.Count)
     43             {
     44                 case 0:
     45                     return Vector3.zero;
     46                 case 1:
     47                     return VisibleThings[0].position;
     48                 default:
     49                     return FindBoundsAndSetBounds().center;
     50             }
     51         }
     52 
     53         private float FindSize()
     54         {
     55             return FindBoundsAndSetBounds().size.x;
     56         }
     57 
     58         private Bounds FindBoundsAndSetBounds()
     59         {
     60             if (VisibleThings.Count == 0)
     61             {
     62                 Bounds zeroBounds = new Bounds(Vector3.zero, Vector3.zero);
     63                 return zeroBounds;
     64            
     65             }
     66             Bounds playerBounds = new Bounds(VisibleThings[0].position, Vector3.zero);
     67             foreach (var thing in VisibleThings)
     68             {
     69                 playerBounds.Encapsulate(thing.position);
     70             }
     71             return playerBounds;
     72         }
     73 
     74         public void AddToCameraList(GameObject thing)
     75         {
     76             VisibleThings.Add(thing.transform);
     77         }
     78         public void RemoveFromCameraList(GameObject thing)
     79         {
     80             VisibleThings.Remove(thing.transform);
     81         }
     82     }
     83 }