Files

49 lines
1.3 KiB
C#
Raw Permalink Normal View History

2026-06-04 21:37:43 +03:00
using UnityEngine;
using UnityEngine.UI;
namespace OTG.Lab06.Combat
{
public sealed class FloatingDamage : MonoBehaviour
{
[SerializeField] private Text label;
[SerializeField] private float lifetime = 1.1f;
[SerializeField] private float riseSpeed = 1.2f;
private float remaining;
public void Initialize(float amount)
{
if (label == null)
{
label = GetComponentInChildren<Text>();
}
label.text = Mathf.RoundToInt(amount).ToString();
remaining = lifetime;
}
private void Update()
{
transform.position += Vector3.up * (riseSpeed * Time.deltaTime);
if (Camera.main != null)
{
transform.rotation = Quaternion.LookRotation(transform.position - Camera.main.transform.position, Vector3.up);
}
remaining -= Time.deltaTime;
float alpha = Mathf.Clamp01(remaining / lifetime);
if (label != null)
{
Color color = label.color;
color.a = alpha;
label.color = color;
}
if (remaining <= 0f)
{
Destroy(gameObject);
}
}
}
}