I want to play a attack animation for my 2D game on Android. my player have a attack Time set to 1 time/s , but my animation during 0.5s, so when i want to attack, my animation is played 2 time
i know that i can make longer my animation to have 1sc animated , but the delay between attack can change so the problem will appear again
You have animatorโs attack boolean value set to true for the whole duration of the attack. Animation is shorter than the total attack time, so it repeats.
To avoid that without making the animation itself longer, you can:
Pulse switch attack boolean of the animator to true, then to false in the next frame.
Use trigger type instead of the boolean.
Then, as long as you are attacking, just toggle the attack boolean/trigger value of the animator every second. You can do that either in the update function or in a coroutine, if you feel comfortable using it:
Coroutine attackCoroutine;
public void Attack() {
if (!attacking) {
attackTimeCounter = attackTime;
attacking = true;
myRigidoby.velocity = Vector2.zero;
attackCoroutine = StartCoroutine(AttackIE());
}
}
public void AttackStop() {
if (attacking) {
StopCoroutine(attackCoroutine);
anim.SetBool("Attack", false);
attacking = false;
}
}
IEnumerator AttackIE() {
while (attacking) {
anim.SetBool("Attack", true);
yield return null;
anim.SetBool("Attack", false);
yield return new WaitForSeconds(attackTimeCounter);
}
}