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í 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áè není v dohledu i bìhem patrolování
|
||||
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ùže útoè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áè je pøíliš daleko, vra� se k patrolování
|
||||
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ŠÍ a PØESNÌJŠÍ otáèení k hráè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ýšená rychlost otáèení
|
||||
}
|
||||
|
||||
// Zkus zaútoèit
|
||||
bool attackAttempted = enemyAttack.TryAttack();
|
||||
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
// Otoè se k hráè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);
|
||||
|
||||
// Útok na hráèe
|
||||
if (canAttack && distanceToPlayer <= attackRange)
|
||||
// Zùstaò v attack stavu pokud mùže útoèit nebo se otáèí k hráèi
|
||||
if (!attackAttempted && enemyAttack.CanAttack() && enemyAttack.IsPlayerInAttackRange())
|
||||
{
|
||||
StartCoroutine(Attack());
|
||||
return;
|
||||
}
|
||||
|
||||
// Pokud je hráè pøíliš daleko, pokraèuj v pronásledování
|
||||
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é akce pøi zmìnì 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ále v platné 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ý cíl v okolí výchozí pozice
|
||||
Vector2 randomCircle = Random.insideUnitCircle * patrolRange;
|
||||
patrolTarget = patrolCenter + new Vector3(randomCircle.x, 0, randomCircle.y);
|
||||
|
||||
// Zajisti, ž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 útoku
|
||||
if (animator != null)
|
||||
{
|
||||
animator.SetTrigger("Attack");
|
||||
}
|
||||
|
||||
// Zde mùžeš pøidat logiku poškození hráèe
|
||||
Debug.Log("Enemy attacks player!");
|
||||
|
||||
yield return new WaitForSeconds(attackCooldown);
|
||||
canAttack = true;
|
||||
}
|
||||
|
||||
// Voláno pøi smrti nepøítele
|
||||
public void Die()
|
||||
{
|
||||
@@ -231,11 +258,14 @@ public class EnemyMovement : MonoBehaviour
|
||||
agent.isStopped = true;
|
||||
}
|
||||
|
||||
// Zde mùžeš 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