Hi everyone. I have a problem with an animation in Unity. It’s probably something really simple, but I’ve spent a lot of time on it and still don’t know what the issue is. The problem is that the attack animation plays twice every time I use “Fire1”. Below, I’ve included the code and how it looks in the Animator. The triggers in the Animator are supposed to call the corresponding functions in the script. Can someone point out where I might be going wrong?
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
public Animator animator; // Animator postaci
public float attackRange = 1.5f; // Zasięg ataku
public LayerMask enemyLayer; // Warstwa, na której znajdują się przeciwnicy
private bool canDamage = false; // Flaga do kontrolowania, czy możemy zadawać obrażenia
private bool isAttacking = false; // Flaga do sprawdzenia, czy atak trwa
void Start()
{
animator.ResetTrigger("StartAttack");
animator.ResetTrigger("EndAttack");
}
private void Update()
{
if (Input.GetButtonDown("Fire1") && !isAttacking)
{
Attack();
}
}
void Attack()
{
if (animator != null && !isAttacking)
{
Debug.Log("Atak rozpoczęty");
animator.SetTrigger("StartAttack");
isAttacking = true;
}
}
public void DealDamage()
{
if (canDamage)
{
Collider[] hitEnemies = Physics.OverlapSphere(transform.position, attackRange, enemyLayer);
Debug.Log($"Found {hitEnemies.Length} enemies in range");
foreach (Collider enemy in hitEnemies)
{
Debug.Log($"Attacking {enemy.name}");
EnemyHealth enemyHealth = enemy.GetComponent<EnemyHealth>();
if (enemyHealth != null)
{
enemyHealth.TakeDamage(10);
}
}
}
}
public void StartAttack()
{
canDamage = true;
}
public void EndAttack()
{
canDamage = false;
isAttacking = false;
animator.ResetTrigger("StartAttack");
animator.SetTrigger("EndAttack");
}
public void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
}
}


