- Home /
How to stop attack animation midway.
In my 2d game I have my enemies attack once the player is in attack range. However, when the player goes out of attack range, the damage is still dealt. I have the damage calculations tied with the attack animations of the enemy so the player is only dealt damage at a specific point of the attack animation. How can I stop the attack animation once the player is out of attack range during the animation?
Here is my code for the enemy attacks:
private int damageAmount;
private int attackNum = 1; //attack index
private Animator anim;
private bool inAttackRange;
private bool isNear;
private float timeBtwAttack;
private float currentDistance;
[SerializeField] private float distance = 2f;
[SerializeField] private float startTimeBtwAttack;
[SerializeField] private Transform attackPos;
[SerializeField] private LayerMask whoIsPlayer;
[SerializeField] private float attackRangeX, attackRangeY;
[SerializeField] private int attack1, attack2, attack3;
[SerializeField] private GameObject player;
private EnemyPatrol patrol;
private bool isAttacking = false;
private void Awake()
{
anim = GetComponent<Animator>();
patrol = GetComponent<EnemyPatrol>();
timeBtwAttack = startTimeBtwAttack;
}
private void Update()
{
//is player in attack range
inAttackRange = Physics2D.OverlapBox(attackPos.position, new Vector2(attackRangeX, attackRangeY), 0, whoIsPlayer);
currentDistance = Mathf.Abs(transform.position.x - player.transform.position.x);
//check if distance from player is near
if(currentDistance > distance)
{
isNear = false;
} else
{
isNear = true;
}
if (timeBtwAttack <= 0)
{
if (inAttackRange && isNear)
{
patrol.enabled = false;
anim.Play("attack" + attackNum);
timeBtwAttack = startTimeBtwAttack;
}
else
{
patrol.enabled = true;
}
}
else
{
timeBtwAttack -= Time.deltaTime;
}
}
private void DealDamage()
{
PlayerHealth.instance.TakeDamage(damageAmount);
attackNum++;
if (attackNum == 1)
{
damageAmount = attack1;
}
else if (attackNum == 2)
{
damageAmount = attack2;
}
else if (attackNum == 3)
{
damageAmount = attack3;
}
else if (attackNum > 3)
{
attackNum = 1;
}
}
I see pros do it and higher MMR (5k?)people do it too, I have quick attack turned on but if I click 'a' on the same target it just keeps auto attacking but if I click on a different target the animation stops. I've tried the 'h' button and that doesn't work either. Do these people just click on different creeps until the timing is right? Or am I missing something completely?
Answer by JethRow · Mar 21 at 07:49 PM
Hello, Create an Animation event at the point u think it would be ideal to send the damage to the player, at that point create a function that will check if the player is near enough, either with colliders, checksphere, raycast or distance vector u think would be ideal, if player is in range at that point, send dmg, if not then dont :) I hope I helped!
Your answer
![](https://koobas.hobune.stream/wayback/20220613054447im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is my character taking damage when attacking & why is it attacking multiple times per hit? 2 Answers
2D Player Movement 0 Answers
Why the Unity 3D Tutorials don't work with Unity 3? 2 Answers
Enemy Movement and Pacing 1 Answer
How do you get your character to look the way you are walking? 1 Answer