You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. [RequireComponent(typeof(TextMeshPro))]
  6. public class MoveAndFade : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private float AnimationTime;
  10. [SerializeField]
  11. [Tooltip("End point in local position")]
  12. private Vector3 EndPoint;
  13. [SerializeField]
  14. private AnimationCurve FadeCurve;
  15. private TextMeshPro text;
  16. private void Start()
  17. {
  18. text = GetComponent<TextMeshPro>();
  19. StartCoroutine(FadeAndMove());
  20. }
  21. private IEnumerator FadeAndMove()
  22. {
  23. Vector3 startPoint = transform.position;
  24. Vector3 endPoint = transform.position + EndPoint;
  25. float startAlpha = text.color.a;
  26. float elapsedTime = 0;
  27. while(elapsedTime < AnimationTime)
  28. {
  29. float ratio = elapsedTime / AnimationTime;
  30. float fadeValue = Mathf.Lerp(startAlpha,0.0f, FadeCurve.Evaluate(ratio));
  31. text.color = new Color(text.color.r, text.color.g, text.color.b, fadeValue);
  32. transform.position = Vector3.Lerp(startPoint, endPoint, ratio);
  33. yield return new WaitForEndOfFrame();
  34. elapsedTime += Time.deltaTime;
  35. }
  36. Destroy(gameObject);
  37. }
  38. }