using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform followTransform;
public BoxCollider2D MapBounds;
private float xMin, xMax, yMin, yMax;
private float camX, camY;
private float camOrthsize;
private float cameraRatio;
private Camera MainCam;
private void Start()
{
xMin = MapBounds.bounds.min.x;
xMax = MapBounds.bounds.max.x;
yMin = MapBounds.bounds.min.y;
yMax = MapBounds.bounds.max.y;
MainCam = GetComponent<Camera>();
camOrthsize = MainCam.orthographicSize;
cameraRatio = (xMax + camOrthsize) / 2.0f;
}
void FixedUpdate()
{
camY = Mathf.Clamp(followTransform.position.y, yMin + camOrthsize, yMax - camOrthsize);
camX = Mathf.Clamp(followTransform.position.x, xMin + cameraRatio, xMax - cameraRatio);
this.transform.position = new Vector3(camX, camY, this.transform.position.z);
}
}