Neues Projekt
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyHealth : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private int startingHealth = 3;
|
||||
|
||||
private int currentHealth;
|
||||
private Animator anim;
|
||||
private bool dead;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
currentHealth = startingHealth;
|
||||
anim = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
public void TakeDamage(int damage)
|
||||
{
|
||||
if (dead) return;
|
||||
|
||||
currentHealth -= damage;
|
||||
|
||||
anim.SetTrigger("hurt");
|
||||
|
||||
if (currentHealth <= 0)
|
||||
{
|
||||
Die();
|
||||
Debug.Log("Enemy is down");
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
dead = true;
|
||||
|
||||
anim.SetTrigger("die");
|
||||
|
||||
// Enemy deaktivieren
|
||||
GetComponent<MeleeEnemy>().enabled = false;
|
||||
|
||||
// Collider ausschalten
|
||||
GetComponent<Collider2D>().enabled = false;
|
||||
|
||||
// Optional: Rigidbody stoppen
|
||||
Rigidbody2D rb = GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
rb.linearVelocity = Vector2.zero;
|
||||
|
||||
Destroy(gameObject, 1.5f);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user