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.
 
 
 
 
 
 

48 lines
1.2 KiB

using System.Collections;
using UnityEngine;
using TMPro;
[RequireComponent(typeof(TextMeshPro))]
public class MoveAndFade : MonoBehaviour
{
[SerializeField]
private float AnimationTime;
[SerializeField]
[Tooltip("End point in local position")]
private Vector3 EndPoint;
[SerializeField]
private AnimationCurve FadeCurve;
private TextMeshPro text;
private void Start()
{
text = GetComponent<TextMeshPro>();
StartCoroutine(FadeAndMove());
}
private IEnumerator FadeAndMove()
{
Vector3 startPoint = transform.position;
Vector3 endPoint = transform.position + EndPoint;
float startAlpha = text.color.a;
float elapsedTime = 0;
while(elapsedTime < AnimationTime)
{
float ratio = elapsedTime / AnimationTime;
float fadeValue = Mathf.Lerp(startAlpha,0.0f, FadeCurve.Evaluate(ratio));
text.color = new Color(text.color.r, text.color.g, text.color.b, fadeValue);
transform.position = Vector3.Lerp(startPoint, endPoint, ratio);
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
}
Destroy(gameObject);
}
}