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:
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 0c4ecdc10844c394b92ccd08ee36d635
|
guid: 8379cbdc96f21cc47a95cab23068d69a
|
||||||
NativeFormatImporter:
|
NativeFormatImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
mainObjectFileID: 11400000
|
mainObjectFileID: 11400000
|
||||||
|
|||||||
@@ -79,8 +79,11 @@ MonoBehaviour:
|
|||||||
agent: {fileID: -7480623267348950463}
|
agent: {fileID: -7480623267348950463}
|
||||||
player: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
|
player: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
|
||||||
updatePathInterval: 0.5
|
updatePathInterval: 0.5
|
||||||
attackRange: 2
|
roomCheckInterval: 2
|
||||||
attackCooldown: 1
|
attackRange: 3
|
||||||
|
sightRange: 50
|
||||||
|
patrolRange: 5
|
||||||
|
enemyAttack: {fileID: 5415715946912615516}
|
||||||
animator: {fileID: 0}
|
animator: {fileID: 0}
|
||||||
--- !u!114 &8002523264253901019
|
--- !u!114 &8002523264253901019
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
@@ -109,10 +112,13 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 333830e3ba10f2942b70a753c4c281de, type: 3}
|
m_Script: {fileID: 11500000, guid: 333830e3ba10f2942b70a753c4c281de, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
attackRange: 5
|
attackRange: 3
|
||||||
attackRate: 2
|
attackRate: 2
|
||||||
attackDamage: 10
|
attackDamage: 10
|
||||||
attackAngle: 45
|
attackAngle: 45
|
||||||
|
player: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
|
||||||
|
animator: {fileID: 0}
|
||||||
|
agent: {fileID: -7480623267348950463}
|
||||||
--- !u!65 &146755941370531150
|
--- !u!65 &146755941370531150
|
||||||
BoxCollider:
|
BoxCollider:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@@ -128,11 +134,11 @@ BoxCollider:
|
|||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
m_Bits: 0
|
m_Bits: 0
|
||||||
m_LayerOverridePriority: 0
|
m_LayerOverridePriority: 0
|
||||||
m_IsTrigger: 1
|
m_IsTrigger: 0
|
||||||
m_ProvidesContacts: 0
|
m_ProvidesContacts: 0
|
||||||
m_Enabled: 1
|
m_Enabled: 1
|
||||||
serializedVersion: 3
|
serializedVersion: 3
|
||||||
m_Size: {x: 4.4725122, y: 2.472512, z: 4.2375846}
|
m_Size: {x: 2, y: 1, z: 2}
|
||||||
m_Center: {x: 0, y: 0.6187928, z: 0}
|
m_Center: {x: 0, y: 0.6187928, z: 0}
|
||||||
--- !u!195 &-7480623267348950463
|
--- !u!195 &-7480623267348950463
|
||||||
NavMeshAgent:
|
NavMeshAgent:
|
||||||
|
|||||||
@@ -160,7 +160,8 @@ MonoBehaviour:
|
|||||||
CorridorStraight: {fileID: 8047827979703692770, guid: 1a5d554c0c76caf4195cae47e098b79d, type: 3}
|
CorridorStraight: {fileID: 8047827979703692770, guid: 1a5d554c0c76caf4195cae47e098b79d, type: 3}
|
||||||
CorridorStraightUnlit: {fileID: 2016417306107577256, guid: 92d9025262a022a499862d352c2724ee, type: 3}
|
CorridorStraightUnlit: {fileID: 2016417306107577256, guid: 92d9025262a022a499862d352c2724ee, type: 3}
|
||||||
DoorCorridor: {fileID: 5603417387143431118, guid: 9eccbf5a500a0d2429e6008a713a49fd, type: 3}
|
DoorCorridor: {fileID: 5603417387143431118, guid: 9eccbf5a500a0d2429e6008a713a49fd, type: 3}
|
||||||
layout: {fileID: 11400000, guid: 0c4ecdc10844c394b92ccd08ee36d635, type: 2}
|
layout: {fileID: 11400000, guid: 8379cbdc96f21cc47a95cab23068d69a, type: 2}
|
||||||
|
useGlobalNavMesh: 1
|
||||||
RoomDistance: 40
|
RoomDistance: 40
|
||||||
--- !u!4 &23489964
|
--- !u!4 &23489964
|
||||||
Transform:
|
Transform:
|
||||||
|
|||||||
@@ -1,52 +1,193 @@
|
|||||||
|
using System.Collections;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.AI;
|
||||||
|
|
||||||
public class EnemyAttack : MonoBehaviour
|
public class EnemyAttack : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
[Header("Attack Settings")]
|
||||||
[SerializeField] private float attackRange = 5f;
|
[SerializeField] private float attackRange = 2f;
|
||||||
[SerializeField] private float attackRate = 2f;
|
[SerializeField] private float attackRate = 2f;
|
||||||
[SerializeField] private float attackDamage = 10f;
|
[SerializeField] private float attackDamage = 10f;
|
||||||
[SerializeField] private float attackAngle = 45f;
|
[SerializeField] private float attackAngle = 45f;
|
||||||
|
|
||||||
private float lastAttackTime = 0f;
|
[Header("References")]
|
||||||
|
[SerializeField] private Transform player;
|
||||||
|
[SerializeField] private Animator animator;
|
||||||
|
[SerializeField] private NavMeshAgent agent;
|
||||||
|
|
||||||
|
private float lastAttackTime = 0f;
|
||||||
|
private bool canAttack = true;
|
||||||
|
private bool isAttacking = false;
|
||||||
|
private float attackCooldownTimer = 0f;
|
||||||
|
|
||||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
|
// Najdi reference
|
||||||
|
GameObject playerObject = GameObject.FindWithTag("Player");
|
||||||
|
if (playerObject != null)
|
||||||
|
{
|
||||||
|
player = playerObject.transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (animator == null) animator = GetComponent<Animator>();
|
||||||
|
if (agent == null) agent = GetComponent<NavMeshAgent>();
|
||||||
|
|
||||||
|
lastAttackTime = -attackRate; // Umo<6D>n<EFBFBD> <20>tok hned na za<7A><61>tku
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
}
|
// Update cooldown timeru
|
||||||
|
if (!canAttack)
|
||||||
private void FixedUpdate()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnTriggerEnter(Collider other)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnTriggerStay(Collider other)
|
|
||||||
{
|
|
||||||
var player = other.GetComponent<PlayerMovement>();
|
|
||||||
|
|
||||||
if (player != null && Time.time - lastAttackTime > attackRate)
|
|
||||||
{
|
{
|
||||||
var health = other.GetComponent<HealthManager>();
|
attackCooldownTimer -= Time.deltaTime;
|
||||||
if (health != null)
|
if (attackCooldownTimer <= 0f)
|
||||||
{
|
{
|
||||||
health.ModifyHealth(-attackDamage);
|
canAttack = true;
|
||||||
Debug.Log("Enemy attacked! Next attack in: " + (1f / attackRate) + " seconds");
|
|
||||||
lastAttackTime = Time.time;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTriggerExit(Collider other)
|
// Metoda pro pokus o <20>tok - vol<6F>na z EnemyMovement
|
||||||
|
public bool TryAttack()
|
||||||
{
|
{
|
||||||
|
if (player == null || !canAttack || isAttacking)
|
||||||
|
{
|
||||||
|
Debug.Log($"Attack failed: player={player != null}, canAttack={canAttack}, isAttacking={isAttacking}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kontrola vzd<7A>lenosti a <20>hlu k hr<68><72>i
|
||||||
|
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||||
|
bool inRange = distanceToPlayer <= attackRange;
|
||||||
|
bool inAngle = IsPlayerInAttackAngle();
|
||||||
|
bool cooldownReady = Time.time - lastAttackTime >= attackRate;
|
||||||
|
|
||||||
|
Debug.Log($"TryAttack: range={inRange}, angle={inAngle}, cooldown={cooldownReady}, distance={distanceToPlayer}");
|
||||||
|
|
||||||
|
if (inRange && cooldownReady)
|
||||||
|
{
|
||||||
|
StartCoroutine(PerformAttack());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug.Log("Attack conditions not met");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsPlayerInAttackAngle()
|
||||||
|
{
|
||||||
|
if (player == null) return false;
|
||||||
|
|
||||||
|
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||||
|
|
||||||
|
// Ignoruj Y osu pro v<>po<70>et <20>hlu
|
||||||
|
directionToPlayer.y = 0;
|
||||||
|
Vector3 forward = transform.forward;
|
||||||
|
forward.y = 0;
|
||||||
|
|
||||||
|
float angle = Vector3.Angle(forward, directionToPlayer);
|
||||||
|
|
||||||
|
// V<>t<EFBFBD><74> tolerance <20>hlu (zv<7A><76>eno z 22.5<EFBFBD> na 60<36>)
|
||||||
|
return angle <= attackAngle; // Pou<6F><75>v<EFBFBD>me cel<65> attackAngle, ne polovinu
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsPlayerInAttackRange()
|
||||||
|
{
|
||||||
|
if (player == null) return false;
|
||||||
|
|
||||||
|
float distance = Vector3.Distance(transform.position, player.position);
|
||||||
|
bool inRange = distance <= attackRange * 1.1f;
|
||||||
|
bool inAngle = IsPlayerInAttackAngle();
|
||||||
|
|
||||||
|
Debug.Log($"AttackRange Check - Distance: {distance}/{attackRange}, InRange: {inRange}, Angle: {inAngle}");
|
||||||
|
|
||||||
|
return inRange && inAngle;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerator PerformAttack()
|
||||||
|
{
|
||||||
|
isAttacking = true;
|
||||||
|
canAttack = false;
|
||||||
|
lastAttackTime = Time.time;
|
||||||
|
attackCooldownTimer = attackRate;
|
||||||
|
|
||||||
|
// Ulo<6C> p<>vodn<64> stav agenta
|
||||||
|
bool wasStopped = agent.isStopped;
|
||||||
|
|
||||||
|
// Zastav pohyb b<>hem <20>toku
|
||||||
|
agent.isStopped = true;
|
||||||
|
|
||||||
|
// Spustit animaci <20>toku
|
||||||
|
if (animator != null)
|
||||||
|
{
|
||||||
|
animator.SetTrigger("Attack");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Po<50>kej chv<68>li p<>ed aplikov<6F>n<EFBFBD>m po<70>kozen<65>
|
||||||
|
yield return new WaitForSeconds(0.3f);
|
||||||
|
|
||||||
|
// Aplikuj po<70>kozen<65>, pokud je hr<68><72> st<73>le v dosahu
|
||||||
|
if (player != null && Vector3.Distance(transform.position, player.position) <= attackRange * 1.2f)
|
||||||
|
{
|
||||||
|
var health = player.GetComponent<HealthManager>();
|
||||||
|
if (health != null)
|
||||||
|
{
|
||||||
|
health.ModifyHealth(-attackDamage);
|
||||||
|
Debug.Log("Enemy attacked player for " + attackDamage + " damage!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Po<50>kej na dokon<6F>en<65> animace
|
||||||
|
yield return new WaitForSeconds(0.7f);
|
||||||
|
|
||||||
|
// Obnov pohyb pouze pokud nebyl p<>vodn<64> zastaven<65>
|
||||||
|
if (!wasStopped)
|
||||||
|
{
|
||||||
|
agent.isStopped = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
isAttacking = false;
|
||||||
|
|
||||||
|
// canAttack se nastav<61> v Update pomoc<6F> timeru
|
||||||
|
Debug.Log("Attack finished, cooldown started");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Metoda pro vizu<7A>ln<6C> reprezentaci <20>to<74>n<EFBFBD>ho <20>hlu
|
||||||
|
private void OnDrawGizmosSelected()
|
||||||
|
{
|
||||||
|
Gizmos.color = Color.red;
|
||||||
|
Gizmos.DrawWireSphere(transform.position, attackRange);
|
||||||
|
|
||||||
|
Gizmos.color = Color.yellow;
|
||||||
|
Vector3 leftDir = Quaternion.Euler(0, -attackAngle / 2, 0) * transform.forward;
|
||||||
|
Vector3 rightDir = Quaternion.Euler(0, attackAngle / 2, 0) * transform.forward;
|
||||||
|
|
||||||
|
Gizmos.DrawRay(transform.position, leftDir * attackRange);
|
||||||
|
Gizmos.DrawRay(transform.position, rightDir * attackRange);
|
||||||
|
Gizmos.DrawLine(transform.position + leftDir * attackRange, transform.position + rightDir * attackRange);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public metody pro komunikaci s EnemyMovement
|
||||||
|
public bool CanAttack()
|
||||||
|
{
|
||||||
|
return canAttack && !isAttacking;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool IsAttacking()
|
||||||
|
{
|
||||||
|
return isAttacking;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float GetCooldownProgress()
|
||||||
|
{
|
||||||
|
return Mathf.Clamp01(attackCooldownTimer / attackRate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetAttackState(bool state)
|
||||||
|
{
|
||||||
|
canAttack = state;
|
||||||
|
if (!state) isAttacking = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,15 +12,18 @@ public class EnemyMovement : MonoBehaviour
|
|||||||
|
|
||||||
[Header("Combat")]
|
[Header("Combat")]
|
||||||
public float attackRange = 2f;
|
public float attackRange = 2f;
|
||||||
public float attackCooldown = 1f;
|
|
||||||
public float sightRange = 20f;
|
public float sightRange = 20f;
|
||||||
public float patrolRange = 5f;
|
public float patrolRange = 5f;
|
||||||
private bool canAttack = true;
|
|
||||||
|
[Header("References")]
|
||||||
|
public EnemyAttack enemyAttack;
|
||||||
|
|
||||||
private enum EnemyState { Patrolling, Chasing, Attacking }
|
private enum EnemyState { Patrolling, Chasing, Attacking }
|
||||||
private EnemyState currentState = EnemyState.Patrolling;
|
private EnemyState currentState = EnemyState.Patrolling;
|
||||||
private Vector3 patrolCenter;
|
private Vector3 patrolCenter;
|
||||||
private Vector3 patrolTarget;
|
private Vector3 patrolTarget;
|
||||||
|
private float lastStateChangeTime;
|
||||||
|
private float minAttackStateDuration = 1.0f;
|
||||||
|
|
||||||
[Header("Animation")]
|
[Header("Animation")]
|
||||||
public Animator animator;
|
public Animator animator;
|
||||||
@@ -34,9 +37,16 @@ public class EnemyMovement : MonoBehaviour
|
|||||||
player = playerObject.transform;
|
player = playerObject.transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Najdi EnemyAttack komponentu
|
||||||
|
if (enemyAttack == null)
|
||||||
|
{
|
||||||
|
enemyAttack = GetComponent<EnemyAttack>();
|
||||||
|
}
|
||||||
|
|
||||||
// Nastav v<>choz<6F> pozice
|
// Nastav v<>choz<6F> pozice
|
||||||
patrolCenter = transform.position;
|
patrolCenter = transform.position;
|
||||||
GenerateNewPatrolTarget();
|
GenerateNewPatrolTarget();
|
||||||
|
lastStateChangeTime = Time.time;
|
||||||
|
|
||||||
// Spustit coroutines
|
// Spustit coroutines
|
||||||
if (agent != null)
|
if (agent != null)
|
||||||
@@ -80,58 +90,119 @@ public class EnemyMovement : MonoBehaviour
|
|||||||
{
|
{
|
||||||
GenerateNewPatrolTarget();
|
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()
|
private void ChaseBehavior()
|
||||||
{
|
{
|
||||||
if (player == null)
|
if (player == null)
|
||||||
{
|
{
|
||||||
currentState = EnemyState.Patrolling;
|
ChangeState(EnemyState.Patrolling);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
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;
|
ChangeState(EnemyState.Attacking);
|
||||||
agent.isStopped = true;
|
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>
|
ChangeState(EnemyState.Patrolling);
|
||||||
currentState = EnemyState.Patrolling;
|
|
||||||
GenerateNewPatrolTarget();
|
GenerateNewPatrolTarget();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AttackBehavior()
|
private void AttackBehavior()
|
||||||
{
|
{
|
||||||
if (player == null)
|
if (player == null || enemyAttack == null)
|
||||||
{
|
{
|
||||||
currentState = EnemyState.Patrolling;
|
ChangeState(EnemyState.Chasing);
|
||||||
agent.isStopped = false;
|
|
||||||
return;
|
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);
|
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||||
|
|
||||||
// Oto<EFBFBD> se k hr<68><72>i
|
// 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
|
||||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
if (!attackAttempted && enemyAttack.CanAttack() && enemyAttack.IsPlayerInAttackRange())
|
||||||
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());
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pokud je hr<68><72> p<><70>li<6C> daleko, pokra<72>uj v pron<6F>sledov<6F>n<EFBFBD>
|
// 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;
|
ChangeState(EnemyState.Chasing);
|
||||||
agent.isStopped = false;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
while (true)
|
||||||
{
|
{
|
||||||
if (currentState == EnemyState.Chasing && player != null)
|
if (currentState == EnemyState.Chasing && player != null && !enemyAttack.IsAttacking())
|
||||||
{
|
{
|
||||||
agent.SetDestination(player.position);
|
agent.SetDestination(player.position);
|
||||||
}
|
}
|
||||||
else if (currentState == EnemyState.Patrolling)
|
else if (currentState == EnemyState.Patrolling && !enemyAttack.IsAttacking())
|
||||||
{
|
{
|
||||||
agent.SetDestination(patrolTarget);
|
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()
|
private IEnumerator CheckCurrentRoom()
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// Zkontroluj, zda je enemy st<73>le v platn<74> m<>stnosti
|
if (!agent.isOnNavMesh && !enemyAttack.IsAttacking())
|
||||||
if (!agent.isOnNavMesh)
|
|
||||||
{
|
{
|
||||||
Debug.LogWarning("Enemy is off NavMesh, attempting to warp...");
|
|
||||||
agent.Warp(transform.position);
|
agent.Warp(transform.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,11 +238,9 @@ public class EnemyMovement : MonoBehaviour
|
|||||||
|
|
||||||
private void GenerateNewPatrolTarget()
|
private void GenerateNewPatrolTarget()
|
||||||
{
|
{
|
||||||
// Vyber n<>hodn<64> c<>l v okol<6F> v<>choz<6F> pozice
|
|
||||||
Vector2 randomCircle = Random.insideUnitCircle * patrolRange;
|
Vector2 randomCircle = Random.insideUnitCircle * patrolRange;
|
||||||
patrolTarget = patrolCenter + new Vector3(randomCircle.x, 0, randomCircle.y);
|
patrolTarget = patrolCenter + new Vector3(randomCircle.x, 0, randomCircle.y);
|
||||||
|
|
||||||
// Zajisti, <20>e c<>l je na NavMesh
|
|
||||||
NavMeshHit hit;
|
NavMeshHit hit;
|
||||||
if (NavMesh.SamplePosition(patrolTarget, out hit, patrolRange, NavMesh.AllAreas))
|
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
|
// Vol<6F>no p<>i smrti nep<65><70>tele
|
||||||
public void Die()
|
public void Die()
|
||||||
{
|
{
|
||||||
@@ -231,11 +258,14 @@ public class EnemyMovement : MonoBehaviour
|
|||||||
agent.isStopped = true;
|
agent.isStopped = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zde m<><6D>e<EFBFBD> p<>idat animaci smrti atd.
|
if (enemyAttack != null)
|
||||||
|
{
|
||||||
|
enemyAttack.SetAttackState(false);
|
||||||
|
}
|
||||||
|
|
||||||
Destroy(gameObject, 2f);
|
Destroy(gameObject, 2f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pro vizualizaci v editoru
|
|
||||||
private void OnDrawGizmosSelected()
|
private void OnDrawGizmosSelected()
|
||||||
{
|
{
|
||||||
Gizmos.color = Color.red;
|
Gizmos.color = Color.red;
|
||||||
@@ -254,4 +284,21 @@ public class EnemyMovement : MonoBehaviour
|
|||||||
Gizmos.DrawLine(transform.position, patrolTarget);
|
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