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

public class CamFollow : MonoBehaviour {

    public GameObject followed;
    bool speedmode;

    public float speedormul;
    
	// Update is called once per frame
	void Update () { 
        Rigidbody2D f = followed.GetComponent<Rigidbody2D>();
        float speed = 0.25f;
        if (speedormul != 0) speed *= speedormul;

        Vector3 p = Vector3.zero;
        if (f == null)
        {
            p = followed.transform.position;
        } else
        {
            p = followed.transform.position;// + (Vector3)f.velocity/2;
        }
        if (Vector3.Distance(transform.position, p) > 5)
        {
            speedmode = true;
        }
        if (Vector3.Distance(transform.position, p) < 2)
        {
            speedmode = false;
        }
        if (speedmode)
        {
            speed = 1;
        }
        p = Vector3.MoveTowards(transform.position, p, speed);
        transform.position = new Vector3(p.x, p.y, transform.position.z);
    }
}
