using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour
{
    public Transform target;
    public Vector3 offset;

    Vector3 camTargetPos;

    void Start()
    {
        camTargetPos = transform.position;
    }
    
    void FixedUpdate()
    {
        if (target)
        {
            Vector2 transPos = transform.position;
            Vector2 targetPos = target.position;

            Vector2 targetDirection = (targetPos - transPos);

            float interpVelocity = targetDirection.magnitude * 5f;
            camTargetPos = transPos + (targetDirection.normalized * interpVelocity * Time.deltaTime);
            transPos = Vector3.Lerp(transform.position, camTargetPos + offset, 0.25f);
            transform.position = new Vector3(transPos.x, transPos.y, -1);
        }
    }
}