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

public class CameraMovement : MonoBehaviour
{
    public GameObject currentTarget;
    private bool _looking = false;

    public void LookAt(MoveCameraValues values)
    {
        StartCoroutine(LookAtCO(values));
    }

    public void SetTarget(GameObject target)
    {
        currentTarget = target;
    }

    public IEnumerator LookAtCO(MoveCameraValues values)
    {
        _looking = true;
        float timer = 0;
        while (_looking)
        {
            Vector3 direction = currentTarget.transform.position - transform.position;
            direction.y = 0;
            var desireRotation = Quaternion.LookRotation(direction);
            transform.rotation = Quaternion.Slerp(transform.rotation, desireRotation, values.speed * Time.deltaTime);
            yield return null;
            timer += Time.deltaTime;

            if (timer >= values.time)
            {
                _looking = false;
            }
        }
    }
}
