2D Player Animation (part 5)

Adding Attack Animations to our Player

For this article we are going to add an attack animation to our player and also learn how to toggle the animation via “Trigger” parameter.

Objective: Adding an Attack Animation to our player

Once you have slice and divided the animation sheet into individual frames, create a new animation clip and name it “Attack”. Then drag and drop all the frames into the animation window and set the frame rate to 30.

Adding all the frames into the Animation dopesheet and setting the frame rate to 30.

Then in the Animator window, we are going to create a new “Trigger” parameter name “Attack”. Create two sets of transitions from Idle to Attack and Running to Attack. Add the Attack trigger parameter as the condition, and also make sure that the transition to the Attack state must have no “Exit Time” and the Transition Duration has zero.

In the player animation script we are going to create a new public method that will toggle the animation Trigger.

In the player script we are going to create a new input function, and whenever the “E” button is pushed we will toggle the attack animation.

Now when ever we push the E button when the game is playing, the player will be able to swing its sword and do the attack animation.