using UnityEngine; using TMPro; using UnityEngine.UI; using System.Collections; using DG.Tweening; namespace Unity.BossRoom.Gameplay.UI { public class FloatingScore : MonoBehaviour { [SerializeField] private TextMeshProUGUI scoreChangeText; // The text component [SerializeField] private Image backgroundImage; // The background image [SerializeField] private float animationDuration = 1.5f; // How long the animation lasts [SerializeField] private Vector3 floatOffset = new Vector3(0, 2f, 0); // How far upward to move private CanvasGroup canvasGroup; // Used for fading private void Awake() { canvasGroup = gameObject.AddComponent(); // Adds a CanvasGroup for fading } /// /// Initializes the floating score effect. /// /// The amount by which the score changed. public void Initialize(int change) { // Set the text to display the score change, adding a '+' for positive changes. if (change >= 0) { scoreChangeText.text = $"+{change}"; scoreChangeText.fontMaterial = new Material(scoreChangeText.fontSharedMaterial); scoreChangeText.fontMaterial.SetFloat("_OutlineWidth", 0.5f); scoreChangeText.fontMaterial.SetColor("_OutlineColor", Color.green); //backgroundImage.color = new Color(0.66f, 1f, 0.38f, 1f); // Green for increases } else { scoreChangeText.text = $"{change}"; scoreChangeText.fontMaterial = new Material(scoreChangeText.fontSharedMaterial); scoreChangeText.fontMaterial.SetFloat("_OutlineWidth", 0.5f); scoreChangeText.fontMaterial.SetColor("_OutlineColor", Color.red); //backgroundImage.color = new Color(1f, 0.4f, 0.38f, 1f); // Red for decreases } transform.DOLocalMove(Vector3.zero, 0.5f); StartCoroutine(Animate()); } /// /// Animates the floating score effect. /// private IEnumerator Animate() { float elapsedTime = 0f; Vector3 startPosition = transform.position; Vector3 endPosition = startPosition + floatOffset; while (elapsedTime < animationDuration) { float t = elapsedTime / animationDuration; transform.position = Vector3.Lerp(startPosition, endPosition, t); canvasGroup.alpha = Mathf.Lerp(1f, 0f, t/2); elapsedTime += Time.deltaTime; yield return null; } // Ensure final position and alpha values transform.position = endPosition; canvasGroup.alpha = 0f; Destroy(gameObject); // Destroy the effect once the animation is complete } } }