Fix: AI
Fixed problem with attacks on player and navmesh (will need some Quality of life improvements in the future)
This commit is contained in:
@@ -12,15 +12,18 @@ public class EnemyMovement : MonoBehaviour
|
||||
|
||||
[Header("Combat")]
|
||||
public float attackRange = 2f;
|
||||
public float attackCooldown = 1f;
|
||||
public float sightRange = 20f;
|
||||
public float patrolRange = 5f;
|
||||
private bool canAttack = true;
|
||||
|
||||
[Header("References")]
|
||||
public EnemyAttack enemyAttack;
|
||||
|
||||
private enum EnemyState { Patrolling, Chasing, Attacking }
|
||||
private EnemyState currentState = EnemyState.Patrolling;
|
||||
private Vector3 patrolCenter;
|
||||
private Vector3 patrolTarget;
|
||||
private float lastStateChangeTime;
|
||||
private float minAttackStateDuration = 1.0f;
|
||||
|
||||
[Header("Animation")]
|
||||
public Animator animator;
|
||||
@@ -34,9 +37,16 @@ public class EnemyMovement : MonoBehaviour
|
||||
player = playerObject.transform;
|
||||
}
|
||||
|
||||
// Najdi EnemyAttack komponentu
|
||||
if (enemyAttack == null)
|
||||
{
|
||||
enemyAttack = GetComponent<EnemyAttack>();
|
||||
}
|
||||
|
||||
// Nastav v<>choz<6F> pozice
|
||||
patrolCenter = transform.position;
|
||||
GenerateNewPatrolTarget();
|
||||
lastStateChangeTime = Time.time;
|
||||
|
||||
// Spustit coroutines
|
||||
if (agent != null)
|
||||
@@ -80,58 +90,119 @@ public class EnemyMovement : MonoBehaviour
|
||||
{
|
||||
GenerateNewPatrolTarget();
|
||||
}
|
||||
|
||||
// Kontrola, zda hr<68><72> nen<65> v dohledu i b<>hem patrolov<6F>n<EFBFBD>
|
||||
if (player != null && enemyAttack != null && enemyAttack.IsPlayerInAttackRange())
|
||||
{
|
||||
ChangeState(EnemyState.Attacking);
|
||||
}
|
||||
}
|
||||
|
||||
private void ChaseBehavior()
|
||||
{
|
||||
if (player == null)
|
||||
{
|
||||
currentState = EnemyState.Patrolling;
|
||||
ChangeState(EnemyState.Patrolling);
|
||||
return;
|
||||
}
|
||||
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
if (distanceToPlayer <= attackRange)
|
||||
// P<>epni do attack stavu pouze pokud m<><6D>e <20>to<74>it a je v dosahu
|
||||
if (enemyAttack != null && enemyAttack.IsPlayerInAttackRange() && enemyAttack.CanAttack())
|
||||
{
|
||||
currentState = EnemyState.Attacking;
|
||||
agent.isStopped = true;
|
||||
ChangeState(EnemyState.Attacking);
|
||||
return;
|
||||
}
|
||||
else if (distanceToPlayer > sightRange * 1.5f)
|
||||
|
||||
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;
|
||||
ChangeState(EnemyState.Patrolling);
|
||||
GenerateNewPatrolTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private void AttackBehavior()
|
||||
{
|
||||
if (player == null)
|
||||
if (player == null || enemyAttack == null)
|
||||
{
|
||||
currentState = EnemyState.Patrolling;
|
||||
agent.isStopped = false;
|
||||
ChangeState(EnemyState.Chasing);
|
||||
return;
|
||||
}
|
||||
|
||||
// RYCHLEJ<45><4A> a P<>ESN<53>J<EFBFBD><4A> ot<6F><74>en<65> k hr<68><72>i
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
|
||||
// Ignoruj Y osu pro rotaci
|
||||
directionToPlayer.y = 0;
|
||||
|
||||
if (directionToPlayer != Vector3.zero)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(directionToPlayer);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 15f * Time.deltaTime); // Zv<5A><76>en<65> rychlost ot<6F><74>en<65>
|
||||
}
|
||||
|
||||
// Zkus za<7A>to<74>it
|
||||
bool attackAttempted = enemyAttack.TryAttack();
|
||||
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
// Oto<EFBFBD> se k hr<68><72>i
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
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)
|
||||
// Z<EFBFBD>sta<EFBFBD> v attack stavu pokud m<><6D>e <20>to<74>it nebo se ot<6F><74><EFBFBD> k hr<68><72>i
|
||||
if (!attackAttempted && enemyAttack.CanAttack() && enemyAttack.IsPlayerInAttackRange())
|
||||
{
|
||||
StartCoroutine(Attack());
|
||||
return;
|
||||
}
|
||||
|
||||
// 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)
|
||||
if (distanceToPlayer > attackRange * 1.5f && !enemyAttack.IsAttacking())
|
||||
{
|
||||
currentState = EnemyState.Chasing;
|
||||
agent.isStopped = false;
|
||||
ChangeState(EnemyState.Chasing);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator CheckPlayerInRange()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (player != null && !enemyAttack.IsAttacking())
|
||||
{
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
if (currentState == EnemyState.Patrolling && distanceToPlayer <= sightRange)
|
||||
{
|
||||
ChangeState(EnemyState.Chasing);
|
||||
}
|
||||
else if (currentState == EnemyState.Chasing && distanceToPlayer > sightRange * 1.5f)
|
||||
{
|
||||
ChangeState(EnemyState.Patrolling);
|
||||
}
|
||||
else if (currentState == EnemyState.Chasing && distanceToPlayer <= attackRange && enemyAttack.CanAttack())
|
||||
{
|
||||
ChangeState(EnemyState.Attacking);
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.3f);
|
||||
}
|
||||
}
|
||||
|
||||
private void ChangeState(EnemyState newState)
|
||||
{
|
||||
if (currentState == newState) return;
|
||||
|
||||
currentState = newState;
|
||||
lastStateChangeTime = Time.time;
|
||||
|
||||
// Specifick<63> akce p<>i zm<7A>n<EFBFBD> stavu
|
||||
switch (newState)
|
||||
{
|
||||
case EnemyState.Chasing:
|
||||
agent.isStopped = false;
|
||||
break;
|
||||
case EnemyState.Patrolling:
|
||||
agent.isStopped = false;
|
||||
GenerateNewPatrolTarget();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,11 +210,11 @@ public class EnemyMovement : MonoBehaviour
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (currentState == EnemyState.Chasing && player != null)
|
||||
if (currentState == EnemyState.Chasing && player != null && !enemyAttack.IsAttacking())
|
||||
{
|
||||
agent.SetDestination(player.position);
|
||||
}
|
||||
else if (currentState == EnemyState.Patrolling)
|
||||
else if (currentState == EnemyState.Patrolling && !enemyAttack.IsAttacking())
|
||||
{
|
||||
agent.SetDestination(patrolTarget);
|
||||
}
|
||||
@@ -152,37 +223,12 @@ public class EnemyMovement : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator CheckPlayerInRange()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator CheckCurrentRoom()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// Zkontroluj, zda je enemy st<73>le v platn<74> m<>stnosti
|
||||
if (!agent.isOnNavMesh)
|
||||
if (!agent.isOnNavMesh && !enemyAttack.IsAttacking())
|
||||
{
|
||||
Debug.LogWarning("Enemy is off NavMesh, attempting to warp...");
|
||||
agent.Warp(transform.position);
|
||||
}
|
||||
|
||||
@@ -192,11 +238,9 @@ public class EnemyMovement : MonoBehaviour
|
||||
|
||||
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);
|
||||
|
||||
// Zajisti, <20>e c<>l je na NavMesh
|
||||
NavMeshHit hit;
|
||||
if (NavMesh.SamplePosition(patrolTarget, out hit, patrolRange, NavMesh.AllAreas))
|
||||
{
|
||||
@@ -204,23 +248,6 @@ public class EnemyMovement : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator Attack()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// Vol<6F>no p<>i smrti nep<65><70>tele
|
||||
public void Die()
|
||||
{
|
||||
@@ -231,11 +258,14 @@ public class EnemyMovement : MonoBehaviour
|
||||
agent.isStopped = true;
|
||||
}
|
||||
|
||||
// Zde m<><6D>e<EFBFBD> p<>idat animaci smrti atd.
|
||||
if (enemyAttack != null)
|
||||
{
|
||||
enemyAttack.SetAttackState(false);
|
||||
}
|
||||
|
||||
Destroy(gameObject, 2f);
|
||||
}
|
||||
|
||||
// Pro vizualizaci v editoru
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
@@ -254,4 +284,21 @@ public class EnemyMovement : MonoBehaviour
|
||||
Gizmos.DrawLine(transform.position, patrolTarget);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (Application.isPlaying && enemyAttack != null)
|
||||
{
|
||||
GUI.Label(new Rect(10, 30, 300, 20), $"State: {currentState}");
|
||||
GUI.Label(new Rect(10, 50, 300, 20), $"CanAttack: {enemyAttack.CanAttack()}");
|
||||
GUI.Label(new Rect(10, 70, 300, 20), $"IsAttacking: {enemyAttack.IsAttacking()}");
|
||||
GUI.Label(new Rect(10, 90, 300, 20), $"Cooldown: {enemyAttack.GetCooldownProgress():P0}");
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, player.position);
|
||||
GUI.Label(new Rect(10, 110, 300, 20), $"Distance: {distance:F2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user