using UnityEngine; using TMPro; // ข้อความลอย เช่น ตัวเลขความเสียหาย: เคลื่อนขึ้นและค่อย ๆ จางก่อนถูกทำลาย public class FloatingText : MonoBehaviour { public float moveSpeed = 1.5f; public float fadeSpeed = 2f; private TextMeshPro textMesh; private Color textColor; void Awake() { textMesh = GetComponent(); if (textMesh != null) { textColor = textMesh.color; } } public void Setup(float damageAmount) { if (textMesh != null) { textMesh.text = Mathf.RoundToInt(damageAmount).ToString(); // สุ่มกระจายซ้ายขวาเล็กน้อยเพื่อให้ไม่ทับกัน transform.position += new Vector3(Random.Range(-0.5f, 0.5f), Random.Range(0f, 0.5f), 0); } } // ข้อความอื่นที่ไม่ใช่ตัวเลข เช่น BLOCKED public void Setup(string message, Color color) { if (textMesh == null) return; textMesh.text = message; textColor = new Color(color.r, color.g, color.b, 1f); textMesh.color = textColor; transform.position += new Vector3(Random.Range(-0.3f, 0.3f), Random.Range(0f, 0.3f), 0); } // เน้นตัวเลข (ดาเมจหนัก): เปลี่ยนสีและขยาย public void Emphasize(Color color, float scale) { if (textMesh == null) return; textColor = new Color(color.r, color.g, color.b, textColor.a); textMesh.color = textColor; transform.localScale *= scale; } void Update() { transform.position += new Vector3(0, moveSpeed * Time.deltaTime, 0); if (textMesh != null) { textColor.a -= fadeSpeed * Time.deltaTime; textMesh.color = textColor; if (textColor.a <= 0) { Destroy(gameObject); } } } }