Daily progress — Attack Animation
Another day, another player animation! Today we’ll be importing the attack animation, same process as before:
For now, we’ll assume the player can only attack when they’re idle. Since an attack is a fast action that a player ‘fires and forgets’, we can use a trigger as a parameter. So going from idle to attack, we’ll use a trigger named Attack as its condition. exit time is off, and 0 transition. From attack to idle, Has Exit time is on, and nothing else.
We can actually test the trigger by selecting it in play mode:
Looking good!
Now we need to script it, it’s pretty straightforward for the PlayerAnimation script; Just create a public function that triggers the attack.
Then in the Player script, we’ll create a “CheckAttack” function that calls it.
Then place the CheckAttack in your update of course.
I did run into an issue where clicking on the mouse plays the attack animation twice. You’re going to have to create a behaviour script for the attack animation and OnStateExit, use ResetTrigger on the animation.
AttackBehaviour script:
The attack works perfectly now. So if you have any triggered short animations that play more than once, remember to create a behaviour for that animation and use ResetTrigger.
So let’s attack while we’re running!
Going from Run to Attack, we use the attack trigger for the condition, turn off Exit time, and 0 out the transition time.
Going from Attack to Run, Has Exit Time *IS* ticked, but we 0 out the transition duration as well.
And that’s pretty much it.
Everything seems to be working as expected!