Tutorial: Script Course #3 Let's Create a Sword Attack.

In this course, you will learn the basics of scripting in ACTION GAME MAKER.
In the third session, we will create an attack using a sword.

Prerequisites for Starting the Course

Continue using the project from the first and second sessions.
Please open the project and prepare accordingly.

STEP 1: Add a Sword

  1. Open Object_Sampleplayer.
  2. Since the character currently holds a gun, hide it first. Click the :eye: icon next to W005 Gun to hide it.
  3. Instead, equip a sword. Check the file system. Like the gun, there are several swords located in templates>objects>weapons>P_other.
  4. Place your preferred sword as a child of B_Weapon. Here, we will use W_001_sword.png.
  5. The angle looks incorrect, so adjust the properties. Values around x:17, y:-16, and Rotation:48 should work well.
  6. Check the movement in AnimationPlayer. Try playing the 033_Swing animation. If something feels off, make minor adjustments in the properties of W001Sword.
  7. Add an attack hitbox to the sword. Although this object already has an AttackArea set, it is currently positioned at the feet, so move it. Make AttackArea2D a child of B_Weapon.
  8. Adjust the position of AttackArea2D. Move it to align with the sword. Values around x:13, y:-11, and Rotation:-42 should work well.
  9. Adjust the size and position of AttackCollision. Since the Position and Rotation are misaligned, reset them to 0. Also, since the attack range appears slightly narrow, modify the collision shape to make it longer. Note that the x-coordinate will be automatically adjusted according to the collision shape.
  10. Play the Swing animation again. The attack range should now move along with the sword.

Explanation

Attack Range for Sprite Characters
For characters using sprite sheets, it is advisable to configure the attack range to change its position and size according to the animation.
By expanding the Shape property of CollisionShape2D, an input screen to modify the shape size will appear. By inserting this as a keyframe, you can change the collision size within the animation.

STEP2: Visual Script for Sword Attack

  1. Open the Visual Script.
  2. Create a new state called “Sword” and set its animation to “Swing”.
  3. Add an “Attack” action to the Sword state and configure it so that attacks only hit enemy groups.
  4. Link from “Idle” to “Sword” and set the condition to “Y (V on the keyboard) is pressed”.
  5. With the link from “Idle” to “Sword” still selected, press Ctrl+C or use the right-click menu to copy it.
  6. Select “Move”, press Ctrl+V or choose “Paste Link” from the right-click menu, and connect it to “Sword”. This will allow sword attacks while moving.
  7. Finally, create a shortcut link from “Sword” back to “Idle”. Set the transition condition to “Animation finished”.
  8. You have now created a melee attack. Let’s test the behavior. If configured correctly, pressing the Y key on the controller or the V key on the keyboard should trigger a sword attack, allowing you to strike down zombies. However, you may notice a few issues:
  • The sword remains equipped even during shooting, preventing the gun from appearing: You need to implement a switching mechanism between the sword and the gun.
  • Zombies take damage from the sword even without an active attack: This is because the sword’s attack hitbox remains active continuously.

In the next STEP, we will fix these issues.

STEP3: Improve the Visual Script.

  1. Select the Idle state and add 3 actions. The first is ChangeObjectProperty. Set the property of your own node, W005Gun’s Visible, to Off.
  2. The second is also ChangeObjectProperty. Set the property of your own node, W001Sword’s Visible, to Off.
  3. The third is also… ChangeObjectProperty. Set the property of your own node, AttackCollision’s Disabled, to On.
  4. Try a test play. The sword and gun should disappear, and even if you ram into a zombie, the zombie should not take damage. However, since they remain invisible, next we will enable their display.
  5. In the Sword state, do the reverse of the previous steps. Add 2 actions. Using ChangeObjectProperty, set the property of your own node, W001Sword’s Visible, to On.
  6. Using ChangeObjectProperty, set the property of your own node, AttackCollision’s Disabled, to Off.
  7. Let’s try a test play. If set correctly, the sword should only be visible during sword attacks.
  8. Next is the gun. In the Shoot state, add just one action, again using ChangeObjectProperty, and set the property of your own node, W005Gun’s Visible, to On.
  9. Let’s try a test play. The gun should now only be visible when firing.

TIPS

Switching Properties Using Animations
In this course, we switched the visibility of each sprite using the ChangeProperty action, but this behavior can also be performed using Animation.
Since properties can be set as keyframes, please note that the switch will only occur when the animation plays up to that point. If you switch animations midway, the visibility switch may not work correctly.
In fact, some animations, such as 026_Crouch, use animations to switch expression polygons.
For how to use AnimationPlayer, please refer to the Graphics Course.

The fourth session is here: