Advertisement
fuccpuff

Untitled

Jan 29th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. using TMPro;
  2. using UnityEngine;
  3.  
  4. public class HealthManager : MonoBehaviour
  5. {
  6. public int maxHealth = 100;
  7. private int currentHealth;
  8. public TextMeshProUGUI healthText;
  9.  
  10. void Start()
  11. {
  12. currentHealth = maxHealth;
  13. UpdateHealthText();
  14. }
  15.  
  16. void UpdateHealthText()
  17. {
  18. healthText.text = "HP: " + currentHealth.ToString();
  19. }
  20.  
  21. public void TakeDamage(int damage)
  22. {
  23. currentHealth -= damage;
  24. UpdateHealthText();
  25. if (currentHealth <= 0)
  26. {
  27. Die();
  28. }
  29. }
  30.  
  31. void Die()
  32. {
  33. // Die logic
  34. }
  35.  
  36. public void DecreaseHP(int amount)
  37. {
  38. currentHealth -= amount;
  39. UpdateHealthText();
  40. }
  41.  
  42. private void OnTriggerEnter2D(Collider2D collision)
  43. {
  44. if (collision.gameObject.tag == "Enemy")
  45. {
  46. TakeDamage(1); // Предполагается, что урон от врага составляет 1
  47. }
  48. }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement