using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowScript : MonoBehaviour
{
    public GameObject[] followeds;
    private void Update()
    {
        Vector3 wantpos = Vector3.zero;
        float xmin = 1000000;
        float ymin = 1000000;
        float xmax = -1000000;
        float ymax = -1000000;
        for (int i = 0; i < followeds.Length; i++)
        {
            wantpos += followeds[i].transform.position;
            xmin = Mathf.Min(xmin, followeds[i].transform.position.x);
            xmax = Mathf.Max(xmax, followeds[i].transform.position.x);
            ymin = Mathf.Min(ymin, followeds[i].transform.position.y);
            ymax = Mathf.Max(ymax, followeds[i].transform.position.y);
        }
        wantpos /= followeds.Length;
        wantpos = new Vector3(wantpos.x, wantpos.y, -1);
        transform.position = Vector3.Lerp(transform.position, wantpos, Time.deltaTime);

        if (Vector3.Distance(transform.position, wantpos) < 0.05f)
        {
            transform.position = wantpos;
        }

        //transform.position = Vector3.MoveTowards(transform.position, wantpos, 0.01f);
        //print(wantpos);

        //float dist = Mathf.Clamp(Mathf.Max(xmax - xmin, ymax - ymin), 4, 15);
        //GetComponent<Camera>().orthographicSize = Mathf.MoveTowards(GetComponent<Camera>().orthographicSize, dist, 0.1f);
    }
}
