using UnityEngine;
using System.Collections;

public class VanishOnKeyPress : MonoBehaviour {
	
	private bool awaitingTransition = true;
	private TextMesh textMesh;
	private Color startColor;
	private Color finalColor;
	private float contadorDeTiempo;
	[SerializeField] private float tiempoDeTransicion = 2;
	
	private void Awake() {
		textMesh = this.GetComponent<TextMesh>();
		if (textMesh == null) GameObject.Destroy(this.gameObject);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.anyKeyDown && awaitingTransition) {
			awaitingTransition = false;
			startColor = textMesh.color;
			finalColor = new Color(startColor.r, startColor.g, startColor.b, 0);
			contadorDeTiempo = 0;
		}
		if (!awaitingTransition) {
			contadorDeTiempo += Time.deltaTime;
			textMesh.color = Color.Lerp(startColor, finalColor, Mathf.Clamp01(contadorDeTiempo / tiempoDeTransicion));
		}
	}
}
