механики
This commit is contained in:
1 parent
dd5a95a4d3
commit
11d57bec19
129 files changed
+21889
-62
No files matched your search
@@ -0,0 +1,73 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class PlayerHealthUI : MonoBehaviour
|
||||
{
|
||||
[Header("Ссылки")]
|
||||
public PlayerHealth playerHealth;
|
||||
public Slider healthSlider;
|
||||
public Image healthFillImage;
|
||||
public Text healthText;
|
||||
|
||||
[Header("Настройки визуала")]
|
||||
public bool smoothTransition = true;
|
||||
public float smoothSpeed = 5f;
|
||||
public bool changeColorWithHealth = true;
|
||||
public Color highHealthColor = new Color(0.2f, 0.85f, 0.3f, 1f); // Зеленый
|
||||
public Color lowHealthColor = new Color(0.9f, 0.2f, 0.2f, 1f); // Красный
|
||||
|
||||
private float currentFill = 1f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (playerHealth == null)
|
||||
{
|
||||
playerHealth = FindAnyObjectByType<PlayerHealth>();
|
||||
}
|
||||
|
||||
UpdateHealthUI(true);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateHealthUI(false);
|
||||
}
|
||||
|
||||
private void UpdateHealthUI(bool immediate)
|
||||
{
|
||||
if (playerHealth == null) return;
|
||||
|
||||
float maxHp = playerHealth.maxHealth;
|
||||
float currentHp = Mathf.Max(0f, playerHealth.CurrentHealth);
|
||||
float targetFill = maxHp > 0f ? currentHp / maxHp : 0f;
|
||||
|
||||
if (immediate || !smoothTransition)
|
||||
{
|
||||
currentFill = targetFill;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentFill = Mathf.MoveTowards(currentFill, targetFill, smoothSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
if (healthSlider != null)
|
||||
{
|
||||
healthSlider.value = currentFill;
|
||||
}
|
||||
|
||||
if (healthFillImage != null)
|
||||
{
|
||||
healthFillImage.fillAmount = currentFill;
|
||||
|
||||
if (changeColorWithHealth)
|
||||
{
|
||||
healthFillImage.color = Color.Lerp(lowHealthColor, highHealthColor, currentFill);
|
||||
}
|
||||
}
|
||||
|
||||
if (healthText != null)
|
||||
{
|
||||
healthText.text = $"{Mathf.CeilToInt(currentHp)} / {Mathf.CeilToInt(maxHp)}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user