2025-08-02 16:04:33 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using UnityEngine;
|
2025-09-04 16:28:28 +02:00
|
|
|
|
using UnityEngine.AI;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
|
|
|
|
|
public class EnemyMovement : MonoBehaviour
|
|
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
[Header("Navigation")]
|
|
|
|
|
|
public NavMeshAgent agent;
|
|
|
|
|
|
public Transform player;
|
|
|
|
|
|
public float updatePathInterval = 0.5f;
|
|
|
|
|
|
public float roomCheckInterval = 2f;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
[Header("Combat")]
|
|
|
|
|
|
public float attackRange = 2f;
|
|
|
|
|
|
public float attackCooldown = 1f;
|
|
|
|
|
|
public float sightRange = 20f;
|
|
|
|
|
|
public float patrolRange = 5f;
|
|
|
|
|
|
private bool canAttack = true;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private enum EnemyState { Patrolling, Chasing, Attacking }
|
|
|
|
|
|
private EnemyState currentState = EnemyState.Patrolling;
|
|
|
|
|
|
private Vector3 patrolCenter;
|
|
|
|
|
|
private Vector3 patrolTarget;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
[Header("Animation")]
|
|
|
|
|
|
public Animator animator;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private void Start()
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
// Najdi hr<68><72>e
|
2025-08-02 16:04:33 +02:00
|
|
|
|
GameObject playerObject = GameObject.FindWithTag("Player");
|
|
|
|
|
|
if (playerObject != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
player = playerObject.transform;
|
|
|
|
|
|
}
|
2025-09-04 16:28:28 +02:00
|
|
|
|
|
|
|
|
|
|
// Nastav v<>choz<6F> pozice
|
|
|
|
|
|
patrolCenter = transform.position;
|
|
|
|
|
|
GenerateNewPatrolTarget();
|
|
|
|
|
|
|
|
|
|
|
|
// Spustit coroutines
|
|
|
|
|
|
if (agent != null)
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
agent.stoppingDistance = attackRange - 0.2f;
|
|
|
|
|
|
StartCoroutine(UpdatePath());
|
|
|
|
|
|
StartCoroutine(CheckPlayerInRange());
|
|
|
|
|
|
StartCoroutine(CheckCurrentRoom());
|
2025-08-02 16:04:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (agent == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
// Update animac<61>
|
|
|
|
|
|
if (animator != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
animator.SetFloat("Speed", agent.velocity.magnitude);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Stavov<6F> chov<6F>n<EFBFBD>
|
|
|
|
|
|
switch (currentState)
|
|
|
|
|
|
{
|
|
|
|
|
|
case EnemyState.Patrolling:
|
|
|
|
|
|
PatrolBehavior();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case EnemyState.Chasing:
|
|
|
|
|
|
ChaseBehavior();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case EnemyState.Attacking:
|
|
|
|
|
|
AttackBehavior();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void PatrolBehavior()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Pokud je c<>l patrolov<6F>n<EFBFBD> dosa<73>en, vyber nov<6F> c<>l
|
|
|
|
|
|
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
|
|
|
|
|
|
{
|
|
|
|
|
|
GenerateNewPatrolTarget();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private void ChaseBehavior()
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
if (player == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = EnemyState.Patrolling;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
if (distanceToPlayer <= attackRange)
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
currentState = EnemyState.Attacking;
|
|
|
|
|
|
agent.isStopped = true;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
}
|
2025-09-04 16:28:28 +02:00
|
|
|
|
else if (distanceToPlayer > sightRange * 1.5f)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Hr<48><72> je p<><70>li<6C> daleko, vra<72> se k patrolov<6F>n<EFBFBD>
|
|
|
|
|
|
currentState = EnemyState.Patrolling;
|
|
|
|
|
|
GenerateNewPatrolTarget();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AttackBehavior()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (player == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = EnemyState.Patrolling;
|
|
|
|
|
|
agent.isStopped = false;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
|
|
|
|
|
|
|
|
|
|
|
// Oto<74> se k hr<68><72>i
|
2025-08-02 16:04:33 +02:00
|
|
|
|
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
2025-09-04 16:28:28 +02:00
|
|
|
|
Quaternion targetRotation = Quaternion.LookRotation(new Vector3(directionToPlayer.x, 0, directionToPlayer.z));
|
|
|
|
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 5f * Time.deltaTime);
|
|
|
|
|
|
|
|
|
|
|
|
// <20>tok na hr<68><72>e
|
|
|
|
|
|
if (canAttack && distanceToPlayer <= attackRange)
|
|
|
|
|
|
{
|
|
|
|
|
|
StartCoroutine(Attack());
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
// Pokud je hr<68><72> p<><70>li<6C> daleko, pokra<72>uj v pron<6F>sledov<6F>n<EFBFBD>
|
|
|
|
|
|
if (distanceToPlayer > attackRange * 1.2f)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = EnemyState.Chasing;
|
|
|
|
|
|
agent.isStopped = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private IEnumerator UpdatePath()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentState == EnemyState.Chasing && player != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
agent.SetDestination(player.position);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (currentState == EnemyState.Patrolling)
|
|
|
|
|
|
{
|
|
|
|
|
|
agent.SetDestination(patrolTarget);
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
yield return new WaitForSeconds(updatePathInterval);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerator CheckPlayerInRange()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (true)
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
if (player != null && currentState != EnemyState.Attacking)
|
|
|
|
|
|
{
|
|
|
|
|
|
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
|
|
|
|
|
|
|
|
|
|
|
if (distanceToPlayer <= sightRange && distanceToPlayer > attackRange)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = EnemyState.Chasing;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (distanceToPlayer <= attackRange)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = EnemyState.Attacking;
|
|
|
|
|
|
agent.isStopped = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
2025-08-02 16:04:33 +02:00
|
|
|
|
}
|
2025-09-04 16:28:28 +02:00
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private IEnumerator CheckCurrentRoom()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Zkontroluj, zda je enemy st<73>le v platn<74> m<>stnosti
|
|
|
|
|
|
if (!agent.isOnNavMesh)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("Enemy is off NavMesh, attempting to warp...");
|
|
|
|
|
|
agent.Warp(transform.position);
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
yield return new WaitForSeconds(roomCheckInterval);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private void GenerateNewPatrolTarget()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Vyber n<>hodn<64> c<>l v okol<6F> v<>choz<6F> pozice
|
|
|
|
|
|
Vector2 randomCircle = Random.insideUnitCircle * patrolRange;
|
|
|
|
|
|
patrolTarget = patrolCenter + new Vector3(randomCircle.x, 0, randomCircle.y);
|
2025-08-02 16:04:33 +02:00
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
// Zajisti, <20>e c<>l je na NavMesh
|
|
|
|
|
|
NavMeshHit hit;
|
|
|
|
|
|
if (NavMesh.SamplePosition(patrolTarget, out hit, patrolRange, NavMesh.AllAreas))
|
|
|
|
|
|
{
|
|
|
|
|
|
patrolTarget = hit.position;
|
|
|
|
|
|
}
|
2025-08-02 16:04:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
private IEnumerator Attack()
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
canAttack = false;
|
|
|
|
|
|
|
|
|
|
|
|
// Spustit animaci <20>toku
|
|
|
|
|
|
if (animator != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
animator.SetTrigger("Attack");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Zde m<><6D>e<EFBFBD> p<>idat logiku po<70>kozen<65> hr<68><72>e
|
|
|
|
|
|
Debug.Log("Enemy attacks player!");
|
|
|
|
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(attackCooldown);
|
|
|
|
|
|
canAttack = true;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
// Vol<6F>no p<>i smrti nep<65><70>tele
|
|
|
|
|
|
public void Die()
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
StopAllCoroutines();
|
|
|
|
|
|
|
|
|
|
|
|
if (agent != null)
|
2025-08-02 16:04:33 +02:00
|
|
|
|
{
|
2025-09-04 16:28:28 +02:00
|
|
|
|
agent.isStopped = true;
|
2025-08-02 16:04:33 +02:00
|
|
|
|
}
|
2025-09-04 16:28:28 +02:00
|
|
|
|
|
|
|
|
|
|
// Zde m<><6D>e<EFBFBD> p<>idat animaci smrti atd.
|
|
|
|
|
|
Destroy(gameObject, 2f);
|
2025-08-02 16:04:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 16:28:28 +02:00
|
|
|
|
// Pro vizualizaci v editoru
|
|
|
|
|
|
private void OnDrawGizmosSelected()
|
|
|
|
|
|
{
|
|
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
|
|
Gizmos.DrawWireSphere(transform.position, attackRange);
|
|
|
|
|
|
|
|
|
|
|
|
Gizmos.color = Color.yellow;
|
|
|
|
|
|
Gizmos.DrawWireSphere(transform.position, sightRange);
|
|
|
|
|
|
|
|
|
|
|
|
Gizmos.color = Color.blue;
|
|
|
|
|
|
Gizmos.DrawWireSphere(patrolCenter, patrolRange);
|
|
|
|
|
|
|
|
|
|
|
|
if (Application.isPlaying)
|
|
|
|
|
|
{
|
|
|
|
|
|
Gizmos.color = Color.green;
|
|
|
|
|
|
Gizmos.DrawSphere(patrolTarget, 0.3f);
|
|
|
|
|
|
Gizmos.DrawLine(transform.position, patrolTarget);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|