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,52 +1,193 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class EnemyAttack : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private float attackRange = 5f;
|
||||
[Header("Attack Settings")]
|
||||
[SerializeField] private float attackRange = 2f;
|
||||
[SerializeField] private float attackRate = 2f;
|
||||
[SerializeField] private float attackDamage = 10f;
|
||||
[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()
|
||||
{
|
||||
// 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()
|
||||
{
|
||||
}
|
||||
|
||||
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)
|
||||
// Update cooldown timeru
|
||||
if (!canAttack)
|
||||
{
|
||||
var health = other.GetComponent<HealthManager>();
|
||||
if (health != null)
|
||||
attackCooldownTimer -= Time.deltaTime;
|
||||
if (attackCooldownTimer <= 0f)
|
||||
{
|
||||
health.ModifyHealth(-attackDamage);
|
||||
Debug.Log("Enemy attacked! Next attack in: " + (1f / attackRate) + " seconds");
|
||||
lastAttackTime = Time.time;
|
||||
canAttack = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user