Objective: Implement a flaming sword with animation.

Flaming sword animation.

The player can buy a flaming sword from the shop, but no implementation exists. Two custom animations are required for to implement the flaming sword for the player. Select the Sprite for the Player.

Open the animation window and create two new clips from the drop down menu. Call one Fire_Attack and the other Fire_Jump_Swing. Drag the animations Fire_Swing_0-34 or Jump_Fire_Swing0-34, into their respective windows and space them over one second. Copy (Ctrl + C) the Hit_Box animation from Attack into Fire_Attack and from Jump_Swing to Fire_Jump_Swing, as these animations share the same timing. Open the animator window and create transitions from and to Idle and Run for Fire_Attack and from and to Jump for Fire_Jump_Swing. Create a new parameter Flaming_Sword to control transitions.

The transition from Idle -> Fire_Attack is shown below, and occurs when Attack is triggered and Flaming_Sword is true. The Run -> Fire_Attack is similar. The exit transition uses has exit time for the transition. Make sure to change the Idle -> Attack and Run -> Attack transitions to only occur when Flaming_Sword is False.

The transition from Jump -> Fire_Jump_Swing is shown below, and occurs when Attack is triggered and both Jump and Flaming_Sword are true. The exit transition uses has exit time. Make sure to change the Jump_Swing -> Jump transitions to only occur when Flaming_Sword is false.

When the flaming sword is purchased this is recorded in the GameManager so implementation of the flaming sword animation is handled in the PlayerAnimation script by updating the Flaming_Sword animator parameter to true, based upon GameManager.Instance.FlamingSword. The Attack() method is called from the Player script upon controller input, i.e. pressing A button on Android device or left mouse click.

Attack method on PlayerAnimation script.

The flaming sword should do more damage, so modify the the IDamagable interface to include an int pain to be passed to the damage method.

IDamagable interface.

The IDamagable interface is used by Player, Skeleton, Spider & MossGiant scripts, example below. All need to implement this change to avoid errors. Also reduce Health by pain.

Damage implementation example on Spider, but similar implementation elsewhere.

The Player, Skeleton and Moss_Giant all use the Attack script attached to their respective hit boxes to damage IDamagable objects, shown below. If the hit object is not Player then the player is striking and enemy. In which case, increase the pain if the player has purchased the flaming sword. Pass the pain level to hit using their Damage method.

Attack method on Hit_Box game objects.

Lastly the Spider uses and acid ranged attack so modify the SpiderAcid script on the Acid prefab as shown below. The change passes the integer 1 via the Damage method to the Player.

This article uses assets for GameDevHQ Filebase.

Software engineer