using System.Collections.Generic; using System.Linq; using UnityEngine; public class CameraController : MonoBehaviour { private bool isTracking = false; // don't track when parented private bool returningHome = false; // cam is on its way home private float baseCamSize = 7; // this is how "zoomed" the ortho cam is private float camMovementTime = 0.9f; // how much time to switch cam positions private float moveSpeed; private float zoomSpeed; public Transform playerCamHome; // camera's home base public GameObject cameraObj; private Camera cameraComponent; private Vector3 currentCamPosition; private float desiredCamSize; private int positionsSet = 0; // this keeps track of how many cam zones we're in private List roomsActive; public void SetCurrentPosition(Vector3 currentCamPosition, float desiredCamSize) { positionsSet++; // we have entered a cam zone cameraObj.transform.parent = null; float distanceToMove = Vector3.Distance(this.currentCamPosition, currentCamPosition); float amountToZoom = Mathf.Abs(this.desiredCamSize - desiredCamSize); moveSpeed = distanceToMove / camMovementTime; zoomSpeed = amountToZoom / camMovementTime; isTracking = true; returningHome = false; this.currentCamPosition = currentCamPosition; this.desiredCamSize = desiredCamSize; } public void ReturnCameraToHome() { positionsSet--; // we have exited a cam zone // if this number is greater than 1, we have left a zone but are still in another if (positionsSet <= 0) { positionsSet = 0; float distanceToMove = Vector3.Distance(this.currentCamPosition, playerCamHome.position); float amountToZoom = Mathf.Abs(this.desiredCamSize - baseCamSize); moveSpeed = distanceToMove / camMovementTime; zoomSpeed = amountToZoom / camMovementTime; isTracking = false; returningHome = true; currentCamPosition = playerCamHome.position; desiredCamSize = baseCamSize; } } public void AddToRoomsActive(StaticCamRoom room) { roomsActive.Add(room); room.RoomEnter(); } public void RemoveFromRoomsActive(StaticCamRoom room) { List wallsToKeep = new List(); roomsActive.Remove(room); foreach (StaticCamRoom staticCamRoom in roomsActive) { wallsToKeep.AddRange(staticCamRoom.objectsToHide); } room.RoomExit(wallsToKeep); } // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { cameraComponent = cameraObj.GetComponent(); roomsActive = new List(); ReturnCameraToHome(); } // Update is called once per frame void Update() { if (isTracking) { cameraObj.transform.position = Vector3.MoveTowards(cameraObj.transform.position, currentCamPosition, Time.deltaTime * moveSpeed); cameraComponent.orthographicSize = Mathf.MoveTowards(cameraComponent.orthographicSize, desiredCamSize, Time.deltaTime * zoomSpeed); } if (returningHome) { currentCamPosition = playerCamHome.position; cameraObj.transform.position = Vector3.MoveTowards(cameraObj.transform.position, currentCamPosition, Time.deltaTime * moveSpeed); cameraComponent.orthographicSize = Mathf.MoveTowards(cameraComponent.orthographicSize, desiredCamSize, Time.deltaTime * zoomSpeed); if (Vector3.Distance(cameraObj.transform.position, currentCamPosition) < 0.1f) { cameraObj.transform.parent = playerCamHome; cameraObj.transform.localPosition = Vector3.zero; cameraComponent.orthographicSize = baseCamSize; returningHome = false; } } } }