Fixed problem with attacks on player and navmesh (will need some Quality of life improvements in the future)
193 lines
5.7 KiB
C#
193 lines
5.7 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class EnemyAttack : MonoBehaviour
|
|
{
|
|
[Header("Attack Settings")]
|
|
[SerializeField] private float attackRange = 2f;
|
|
[SerializeField] private float attackRate = 2f;
|
|
[SerializeField] private float attackDamage = 10f;
|
|
[SerializeField] private float attackAngle = 45f;
|
|
|
|
[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;
|
|
|
|
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žní útok hned na zaèátku
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// Update cooldown timeru
|
|
if (!canAttack)
|
|
{
|
|
attackCooldownTimer -= Time.deltaTime;
|
|
if (attackCooldownTimer <= 0f)
|
|
{
|
|
canAttack = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Metoda pro pokus o útok - volá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álenosti a úhlu k hráè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èet úhlu
|
|
directionToPlayer.y = 0;
|
|
Vector3 forward = transform.forward;
|
|
forward.y = 0;
|
|
|
|
float angle = Vector3.Angle(forward, directionToPlayer);
|
|
|
|
// Vìtší tolerance úhlu (zvýšeno z 22.5° na 60°)
|
|
return angle <= attackAngle; // Používáme celý 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ž pùvodní stav agenta
|
|
bool wasStopped = agent.isStopped;
|
|
|
|
// Zastav pohyb bìhem útoku
|
|
agent.isStopped = true;
|
|
|
|
// Spustit animaci útoku
|
|
if (animator != null)
|
|
{
|
|
animator.SetTrigger("Attack");
|
|
}
|
|
|
|
// Poèkej chvíli pøed aplikováním poškození
|
|
yield return new WaitForSeconds(0.3f);
|
|
|
|
// Aplikuj poškození, pokud je hráè stá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èkej na dokonèení animace
|
|
yield return new WaitForSeconds(0.7f);
|
|
|
|
// Obnov pohyb pouze pokud nebyl pùvodnì zastavený
|
|
if (!wasStopped)
|
|
{
|
|
agent.isStopped = false;
|
|
}
|
|
|
|
isAttacking = false;
|
|
|
|
// canAttack se nastaví v Update pomocí timeru
|
|
Debug.Log("Attack finished, cooldown started");
|
|
}
|
|
|
|
// Metoda pro vizuální reprezentaci útoèného ú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;
|
|
}
|
|
} |