Thanh máu có hiệu ứng động
C# Code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AnimatedHealthBar : MonoBehaviour
{
public Slider mainHealthSlider; // Thanh máu chính, hiển thị máu hiện tại
public Slider delayedHealthSlider; // Thanh máu phụ, chạy theo sau thanh chính
public float delaySpeed = 2f; // Tốc độ của thanh máu phụ
public void SetMaxHealth(int health)
{
mainHealthSlider.maxValue = health;
mainHealthSlider.value = health;
delayedHealthSlider.maxValue = health;
delayedHealthSlider.value = health;
}
public void SetHealth(int health)
{
mainHealthSlider.value = health;
StartCoroutine(UpdateDelayedSlider(health));
}
private IEnumerator UpdateDelayedSlider(int newHealth)
{
// Chờ một chút trước khi thanh phụ bắt đầu di chuyển
yield return new WaitForSeconds(0.5f);
float timer = 0f;
float startValue = delayedHealthSlider.value;
while (timer < 1f)
{
timer += Time.deltaTime * delaySpeed;
delayedHealthSlider.value = Mathf.Lerp(startValue, newHealth, timer);
yield return null;
}
delayedHealthSlider.value = newHealth;
}
}Tạo một thanh máu có hiệu ứng giảm từ từ và nhấp nháy khi nhận sát thương, mang lại cảm giác mượt mà hơn.