using UnityEngine; public class ZombieController : MonoBehaviour { [Header("Health")] private float maxHealth = 50; private float currentHealth; [Header("Movement")] private float walkSpeed = 1f; private float runSpeed = 2f; private float runDistance = 5f; // od kedy začne bežať private float attackDistance = 1.5f; // od kedy útočí [Header("Attack")] private float attackDamage = 10f; private float attackCooldown = 1f; private float lastAttackTime; private Transform player; private Animator animator; private void Start() { currentHealth = maxHealth; animator = GetComponent(); GameObject playerObject = GameObject.FindGameObjectWithTag("Player"); if (playerObject != null) player = playerObject.transform; else Debug.LogWarning("Zombie nenašiel hráča — skontroluj tag 'Player'"); } private void Update() { if (player == null) return; float distance = Vector3.Distance(transform.position, player.position); if (distance <= attackDistance) { StopMoving(); TryAttack(); } else if (distance <= runDistance) { MoveToPlayer(runSpeed); animator.SetBool("isRunning", true); animator.SetBool("isWalking", false); animator.SetBool("isAttacking", false); } else { MoveToPlayer(walkSpeed); animator.SetBool("isWalking", true); animator.SetBool("isRunning", false); animator.SetBool("isAttacking", false); } } private void MoveToPlayer(float speed) { Vector3 direction = (player.position - transform.position).normalized; transform.position += direction * speed * Time.deltaTime; transform.LookAt(new Vector3(player.position.x, transform.position.y, player.position.z)); } private void StopMoving() { animator.SetBool("isWalking", false); animator.SetBool("isRunning", false); } private void TryAttack() { animator.SetBool("isAttacking", true); if (Time.time - lastAttackTime >= attackCooldown) { lastAttackTime = Time.time; Debug.Log($"{gameObject.name} útočí na hráča za {attackDamage} damage!"); // player.GetComponent()?.TakeDamage(attackDamage); } } public void TakeDamage(float amount) { currentHealth -= amount; Debug.Log($"{gameObject.name} dostal {amount} damage. HP: {currentHealth}/{maxHealth}"); if (currentHealth <= 0) Die(); } private void Die() { Debug.Log($"{gameObject.name} je mŕtvy!"); animator.SetTrigger("Die"); Destroy(gameObject, 2f); } }