twinStick/twinStickCrawler/Assets/Scripts/Creatures/UI/DamageNumber.cs
2024-12-24 21:40:18 +01:00

55 lines
1.8 KiB
C#

using System.Collections;
using TMPro;
using UnityEngine;
using Random = System.Random;
namespace UI.WorldUI
{
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);
Billboard();
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);
Billboard();
yield return null;
}
Destroy(this.gameObject);
}
void Billboard()
{
transform.LookAt(transform.position + mainCamera.transform.rotation * Vector3.forward, mainCamera.transform.rotation * Vector3.up);
}
}
}