Unity 2D: Sword attack animation
Objective: Add a sword attack on mouse left click with animation.
Select the animation window then select the Sprite under Player.
Create new clip using the drop down menu named Attack, saving it in the animations folder. Drag the Reg_Swing_0 to Reg_Swing_34 sprites into the Attack animation. Space the attack out over one second as shown below. The actual attack swing begins at 0:17 and ends at 0:32 seconds, important for later animation timing. Select Attack animation in the project, not animator, window then in the inspector uncheck loop time.
Open the animator window and add a trigger parameter called Attack. Add transitions from Attack animation to and from both Idle and Run.
The Idle -> Attack transition is show below. Has exit time is unchecked and transition duration is 0. Add the Attack trigger as the condition for activating the animation. The Run -> Attack animation is set up the same way.
The Attack -> Idle transition uses has exit time to return to the idle state after 1 second, as shown below.
The Attack -> Run transition occurs when the animation exits after 1 second and the condition Move is > 0.1f.
The attack animation is controlled from the Player script by calling the Attacks() method in void Update.
If the left mouse button has been clicked the Attack() method on the PlayerAnimation script, _playerAnim, is called.
The Attack() method, shown below, sets the Attack trigger to play the sword attack animation in the Sprite animator above, but it also calls an animation on Sword_Arc described below.
To create the sword arc animation, drag Sword_Three_Particle_0 into the hierarchy window, renaming it Sword_Arc. Adjust the position, rotation and scale as shown below.
Make Sword_Arc a child of Player. Open the animation window and select Sword_Arc. Create a new animation Sword_Arc, saving it in the Animations folder. Drag Sword_Three_Particle_0 through Sword_Three_Particle_7 into the animation window. Put Particle_0 at time zero, but space the other sprites from 0:19 to 0:32 seconds. This is timed to correspond with the actual attack swing in the Attack animation.
Open the animator window for the Sword_Arc controller, and create an empty state named None. Set None as the default state. Add a transition to and from the Sword_Arc animation. Create a trigger parameter called SwordAnimation, which is set from the PlayerAnimation Attack method described earlier. Set up the None -> Sword_Arc animation as described for Idle -> Attack, except that SwordAnimation is the trigger condition. The transition for Sword_Arc -> None requires has exit time and control of the transition duration.
The direction for the sword animation is controlled in the PlayerAnimation script within the Move() method based upon player input, move, from the player script. This code flips the sprites X & Y for the Sword_Arc animation and adjusts the rotation using localEulerAngles to provide the correct perspective based upon the player’s last direction of movement.
The Move() method requires access to the Sword_Arc’s animator and sprite renderer components, as shown below.
This article use assets from GameDevHQ Filebase.