using System.Collections; using TMPro; using UnityEngine; using Random = System.Random; public class DamageNumber : MonoBehaviour { [SerializeField] TextMeshProUGUI textMesh; [SerializeField] private float speed; [SerializeField] private float moveSpeed=10f; 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; StartCoroutine(Animate()); } IEnumerator Animate() { textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, 1); transform.LookAt(transform.position - mainCamera.transform.position); yield return new WaitForSeconds(0.2f); 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 this.transform.position = new Vector3(transform.position.x, transform.position.y+moveSpeed*Time.deltaTime, transform.position.z); transform.LookAt(transform.position - mainCamera.transform.position); yield return null; } Destroy(this.gameObject); } }