Files
zumbi-game/Assets/Scripts/ZombieController.cs
T
dkecskes 6ee1c92cc8 Add ZombieAnimator controller and animations
- Created a new Animator Controller for the Zombie character, including states for Idle, Walk, Run, Attack, and Die.
- Defined transitions between states based on conditions such as isWalking, isRunning, isAttacking, and Die.
- Added a new Animations folder and associated meta files for proper asset management.
2026-05-04 13:08:33 +02:00

102 lines
2.9 KiB
C#

using UnityEngine;
public class ZombieController : MonoBehaviour
{
[Header("Health")]
[SerializeField] private float maxHealth = 100f;
private float currentHealth;
[Header("Movement")]
[SerializeField] private float walkSpeed = 2f;
[SerializeField] private float runSpeed = 4f;
[SerializeField] private float runDistance = 5f; // od kedy začne bežať
[SerializeField] private float attackDistance = 1.5f; // od kedy útočí
[Header("Attack")]
[SerializeField] private float attackDamage = 10f;
[SerializeField] private float attackCooldown = 1f;
private float lastAttackTime;
private Transform player;
private Animator animator;
private void Start()
{
currentHealth = maxHealth;
animator = GetComponent<Animator>();
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<PlayerHealth>()?.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);
}
}