Attack while jumping
Objective: Allow the player to attack while jumping with a unique animation.
The Player hierarchy is shown below. Hit_box is a child of Sprite which is a child of Player.
Create a new animation for the Jump_Swing by selecting the Player Sprite, where the animator resides, and create new clip. Save this clip as Jump_Swing and save in Sprites > Animation folder. Drag the Jump_Swing_0 to 34 sprites into the new animation window and space it to play over one second.
The Hit_Box position and rotation is changed to match the sword position, noting the back swing for the jump attack. The box collider 2D is disabled until the actual attack swing. The box collider 2D size and offset is also adjusted. The timing of the animation must also be adjusted.
Assign transitions to and from the Jump_Swing from the Jump state.
The transition from Jump to Jump_Swing occurs when the parameters Jump is true and Attack is triggered.
Notice that the Jump_Swing direction is the opposite of the Attack swing. The Sword_Arc animation needs to match the sword swing direction. Fortunately, the same animation can be used for the grounded Attack and the Sword_Arc by adjusting the flipX and flipY as well as the y rotation of the _swordRenderer within the PlayerAnimation script, described later. Duplicate the Sword_Arc and rename the copy Sword_Arc_Reversed, and adjust the the key points to match when the Hit_Box is activated and inactivated within the Jump_Swing animation above. Do not worry about arcs direction as this will be handled using code. Now assign transitions to the Jump_Swing animation, see below. Create a new bool parameter Jump.
When the Jump parameter is true and SwordAnimation is triggered animator transitions to the Sword_Arc_Reverse; whereas, Jump must be false when the SwordAnimation is triggered for Sword_Arc. The exit transitions use has exit time.
The animation is controlled from the PlayerAnimation script. The player’s facing is determined withing the Move() method, shown below. So the Player Sprite’s _renderer records this information, which is needed to align Sword_Arc animation with the Jump_Swing animation.
The _swordRenderer’s facing only needs to be determined when the player Attack(), as shown below. The logic is a bit complex, first the player’s grounded state must be known which is received from the Player script when they attack. If grounded the flipX, flipY and rotation for the _swordRenderer are set as shown below, only now within the Attack() method based upon the _renderer.flipX. The Jump_Attack is triggered if ungrounded and flips the _swordRenderer direction based upon the the _renderer.flipX only if it has not already been flipped. The _swordAnimator parameter Jump is set to true to trigger the Sword_Arc_Reverse animation. Finally if the player is not already mid swing the SwordAnimation is triggered for the Sword_Arc animator and the Attack is triggered in the Sprite animator.
The player can now attack while jumping in addition to while standing on the ground. While a jump attack appears to be simple, the code is more complex because it needs to recognize which direction the player is facing and if the player is jumping to align the animation. The combination of possibilities exponentially increase code complexity.