pgiz3/Assets/Scripts/PlayerHealth.cs
2026-03-22 03:00:25 +03:00

49 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI; // Для работы с UI
using TMPro; // Для работы с TextMeshPro
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public TextMeshProUGUI healthText; // Ссылка на текст для отображения здоровья
void Start()
{
currentHealth = maxHealth;
UpdateHealthText();
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
UpdateHealthText();
if (currentHealth <= 0)
{
Die();
}
}
void UpdateHealthText()
{
if (healthText != null)
{
healthText.text = "Здоровье: " + currentHealth;
}
}
void Die()
{
// Пока просто выводим сообщение в консоль и перезапускаем сцену
Debug.Log("Игрок погиб!");
// Для перезапуска сцены нужно добавить: using UnityEngine.SceneManagement;
// SceneManager.LoadScene(SceneManager.GetActiveScene().name);
// Деактивируем управление игрока
GetComponent<PlayerMovement>().enabled = false;
// Можно также показать экран "Game Over"
}
}