using System.Collections; using TMPro; using UnityEngine; using Random = System.Random; public class DamageNumber : MonoBehaviour { [SerializeField] TextMeshProUGUI textMesh; [SerializeField] private float speed; private Camera mainCamera; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { Random rnd = new Random(); speed += rnd.Next(-10, 10)/100f; mainCamera = Camera.main; transform.LookAt(transform.position - mainCamera.transform.position); //StartCoroutine(Animate()); } IEnumerator Animate() { textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, 1); yield return new WaitForSeconds(0.6f); while (textMesh.color.a > 0) { float newAlpha = textMesh.color.a - Time.deltaTime * speed / 255f; // Reduce alpha in the float range newAlpha = Mathf.Clamp01(newAlpha); // Ensure it remains between 0 and 1 textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, newAlpha); // Assign the new alpha value print(textMesh.color.a); yield return null; } } }