using System;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{

    [Header("Target")] 
    [SerializeField] private Transform targetTransform;

    [Header("FollowSettings")] 
    [SerializeField] private float smoothTime = 0.5f;
    [SerializeField] private Vector3 offset = new Vector3(0f, 8f, -10f);

    private Vector3 _velocity = Vector3.zero;


    private void LateUpdate()
    {
        FollowTarget();
    }

    void FollowTarget()
    {
        if (targetTransform == null) return;
        Vector3 desiredCameraPosition = new Vector3(offset.x + targetTransform.position.x, offset.y, offset.z);
        
        //for a smooth camera movement 
        transform.position = Vector3.SmoothDamp(transform.position, desiredCameraPosition, ref _velocity, smoothTime);
        //transform.position = desiredCameraPosition;
    }
}
