using System.Collections;
using UnityEngine;

public class TextEffect : MonoBehaviour
{
    [SerializeField]
    private TextMesh label = null;
    [SerializeField]
    private Gradient color = new Gradient();

    public void Init(string text, float time, Vector3 position, Vector3 direction)
    {
        transform.position = position;
        label.text = text;
        StartCoroutine(AnimateText(time, position, direction));
    }

    private IEnumerator AnimateText(float time, Vector3 position, Vector3 direction)
    {
        var timer = 0f;
        while (timer < time)
        {
            timer += Time.deltaTime;
            var animation = timer / time;
            label.color = color.Evaluate(animation);
            transform.position = position + direction * animation;
            yield return null;
        }
        Destroy(gameObject);
    }
}
