Reset Attack Animation

Hi Guys, everything in my code works ok, but I just can’t make the animation bool parameter go back to “false”. This means the animation is played correctly one time, but it doesnt reset so at another attack, it doesnt play the animation anymore. I have tried more or less every position to set it back to false, but that always only leads to the fact the animation is not played at all, not even the first time. Does anyone have a suggestion? I am confused as for my walking animation, it worked just fine…I am aware that in the code above I do not set it back :wink:

Thanks

 void Attack(Unit enemy)
    {
        camAnim.SetTrigger("Shake");
        hasAttacked = true;
        animator.SetBool("Attack", true);
        int enemyDamage = attackDamage - enemy.armor;
        int myDamage = enemy.defenseDamage - armor;

        if (enemyDamage >= 1)
        {
            DamageIcon instance = Instantiate(damageIcon, enemy.transform.position, Quaternion.identity);
            instance.Setup(enemyDamage);
            enemy.health -= enemyDamage;
            enemy.UpdateKingHealth();
        }
        if (myDamage >= 1)
        {
            DamageIcon instance = Instantiate(damageIcon, transform.position, Quaternion.identity);
            instance.Setup(myDamage);
            health -= myDamage;
            UpdateKingHealth();
        }
        if (enemy.health <= 0)
        {
            Instantiate(deathEffect, enemy.transform.position, Quaternion.identity);
            Destroy(enemy.gameObject);
            GetWalkableTiles();
            gm.RemoveStatsPanel(enemy);
        }
        if (health <= 0)
        {
            Instantiate(deathEffect, transform.position, Quaternion.identity);
            gm.ResetTiles();
            gm.RemoveStatsPanel(this);
            Destroy(this.gameObject);
        }
        gm.UpdateStatsPanel();
    }

We would need a bit more information to answer this question,

you show us a function where you set the trigger and your boolean straight to true,
do you call this in a update loop or when certain coditions happen etc… :o?

the idea is that the condition to trigger the animation when the player attacks, that is the only condition, it is not called anywhere else. it’s (or it wants to be haha) a turn based hex game, so I thought it would work this way. but I can’t manage to make it false again. I understand it is true because well it is true, but I wonder why I cant false it…sorry I am pretty new to this :slight_smile:

I don’t see any code up there that sets it to false. You would need to do that at least one frame later, or ideally when this attack is done, perhaps with an animation callback, which is a bit over-complicated honestly.

Check this out:

https://docs.unity3d.com/Manual/AnimationParameters.html

The Trigger type of animator parameter might be a more appropriate type of property to use for attacking. It’s like a push-button… bap-and-done.

hey kurt, well to be fair I wrote I know there is nothing in this code :slight_smile:

but, using a trigger has another “bug”, while probably it is the better idea

when using that trigger, I can attack WITH animation, next round WITHOUT the animation (here it transitions back to idle), next round again WITH…and so on…

Is that maybe just a timing thing? Like you’re still in the attack anim, so the trigger doesn’t move it there??

ALSO: you can get event callbacks at various points in animations or animator states… this would let you prevent the attack until you hear that the attack state is over and you’re back to idle. If the attack animation becomes twice as long, magically the code just would wait twice as long.

hm I do not htink so…the animation seems to run through. looking at the animator, it runs from idle to attack, stays there, then when I do the next attack, it goes back to idle. I have an attack parameter both on the transition from idle to attack and the other way round, maybe something here wrong?

I think that’s your problem: you want to configure the attack state so that when its done it transitions naturally back to idle. I think you would just add a condition-less transition back to idle, but with that “Has Exit Time” box checked in the transition.

Ohhhh you can make a condition-less transition when not clicking off has exit time! Thanks man I did not know this (now I see unity even says it lol). Cheers mate!