Files
DougDiggem/Assets/Scripts/CameraController.cs
T

69 lines
2.4 KiB
C#

using UnityEngine;
public class CameraController : MonoBehaviour
{
private float moveSpeed = 50.0f;
private float zoomSpeed = 20.0f;
private bool isTracking = false; // don't track when parented
private bool returningHome = false; // cam is on its way home
private float baseCamSize = 5; // this is how "zoomed" the ortho cam is
public Transform playerCamHome; // camera's home base
public GameObject cameraObj;
private Camera cameraComponent;
private Vector3 currentCamPosition;
private float desiredCamSize;
public void SetCurrentPosition(Vector3 currentCamPosition, float desiredCamSize)
{
isTracking = true;
returningHome = false;
cameraObj.transform.parent = null;
this.currentCamPosition = currentCamPosition;
this.desiredCamSize = desiredCamSize;
}
public void ReturnCameraToHome()
{
isTracking = false;
returningHome = true;
currentCamPosition = playerCamHome.position;
desiredCamSize = baseCamSize;
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
cameraComponent = cameraObj.GetComponent<Camera>();
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;
}
}
}
}