Thông báo nổi (Floating Text)
C# Code
using UnityEngine;
using TMPro;
public class FloatingText : MonoBehaviour
{
public float moveSpeed = 1f;
public float fadeOutTime = 1f;
private TextMeshPro textMesh;
private Color textColor;
void Awake()
{
textMesh = GetComponent<TextMeshPro>();
textColor = textMesh.color;
}
void Update()
{
// Di chuyển chữ lên trên
transform.position += new Vector3(0, moveSpeed * Time.deltaTime, 0);
// Làm mờ dần
textColor.a -= Time.deltaTime / fadeOutTime;
textMesh.color = textColor;
// Hủy đối tượng khi đã mờ hẳn
if (textColor.a <= 0)
{
Destroy(gameObject);
}
}
public void SetText(string text)
{
textMesh.text = text;
}
}
// Cách sử dụng:
// public class DamageDealer : MonoBehaviour
// {
// public GameObject floatingTextPrefab;
// public void DealDamage(int damage)
// {
// GameObject textObject = Instantiate(floatingTextPrefab, transform.position, Quaternion.identity);
// textObject.GetComponent<FloatingText>().SetText(damage.ToString());
// }
// }Tạo ra một đối tượng Text bay lên và mờ dần, thường được sử dụng để hiển thị số sát thương gây ra hoặc điểm số nhận được.