Tutorial: Script Course #3 – Creating a Sword Attack

In this course, you will learn the basic scripting techniques in ACTION GAME MAKER.
In Part 3, we will create a sword attack.


Before You Begin

We will continue using the project from Part 1 and Part 2 of the tutorial series.
Please open that project and get it ready before proceeding.

STEP 1: Add a Sword

  1. Open Object_Sampleplayer.
  2. Since the player is currently holding a gun, let’s hide it first. Click the :eye: icon on W005 Gun to make it invisible.
  3. Next, let’s equip the player with a sword instead. Check the FileSystem. Just like the gun, several sword images can be found in templates > objects > weapons > P_other.
  4. Add your preferred sword as a child of B_Weapon. Here, we’ll use W_001_sword.png.
  5. Adjust its position and rotation since the angle is off. Set x: 17, y: -16, and Rotation: 48 for a better fit.
  6. Use AnimationPlayer to check the animation. Play 033_Swing. If something feels off, fine-tune the properties of W001Sword.
  7. Let’s add an attack hitbox to the sword. The object already has an AttackArea, but it’s currently placed at the player’s feet. Move AttackArea2D to be a child of B_Weapon.
  8. Adjust the position of AttackArea2D to match the sword. A good setting might be x: 13, y: -11, Rotation: -42.
  9. Now adjust the size and position of AttackCollision. Since the position and rotation may be off, reset them both to 0. Also, the default hitbox looks a bit too short, so let’s make the shape longer. Note: adjusting the shape may automatically change the x-coordinate.
  10. Play the Swing animation again. The hitbox should now move along with the sword.

Explanation:

Attack Hitboxes for Sprite Characters
For characters created using sprite sheets, it’s a good idea to adjust the attack hitbox size and position based on the animation.
In the Inspector, expand the Shape property of CollisionShape2D.
You can set keyframes to animate the shape size and position as part of the attack motion.

STEP 2: Visual Script for Sword Attack

  1. Open the Visual Script.
  2. Create a new state called Sword, and set the animation to Swing.
  3. Add an action to set attack properties, and configure it so the attack only hits objects in the Enemy group.
  4. Connect a link from Idle to Sword, and set the condition to trigger when the Y button is pressed (V key on keyboard).
  5. While the link from Idle to Sword is selected, press Ctrl+C or right-click and select Copy.
  6. Select Move, then press Ctrl+V or right-click and choose Paste Link, connecting it to Sword. This allows sword attacks to be performed while moving.
  7. Lastly, create a shortcut link from Sword back to Idle, and set the condition to when the animation ends.

Now the close-range sword attack should be functional. Test it out—if everything is set up correctly, pressing the Y button (V on keyboard) should let the player slash with a sword and defeat zombies.

However, you might notice a couple of issues:

  • The sword is always visible, and even during shooting the gun doesn’t appear. → You’ll need a way to switch between sword and gun.
  • Zombies take damage even when you’re not attacking, just by touching the sword. → This happens because the sword’s hitbox is always active.

In the next step, we’ll address these problems.

STEP 3: Improve the Visual Script

  1. Select the Idle state and add three actions.
    The first is ChangeObjectProperty.
    Set it to turn off the Visible property of the W005Gun node.
  2. The second is also ChangeObjectProperty.
    Set it to turn off the Visible property of the W001Sword node.
  3. The third is again ChangeObjectProperty.
    Set it to turn on the Disabled property of the AttackCollision node.
    This disables the sword’s hitbox.
  4. Now try a test play.
    Both the sword and gun should be hidden, and zombies shouldn’t take damage from body contact.
    However, since they stay hidden, we now need to enable them appropriately.
  5. In the Sword state, reverse the settings.
    Add two actions:
    First, ChangeObjectProperty to turn on the Visible property of W001Sword.
  6. Then, ChangeObjectProperty to turn off the Disabled property of AttackCollision.
    This re-enables the sword’s hitbox only during attack.
  7. Test your game again.
    If set up correctly, the sword will only appear during sword attacks.
  8. Now let’s set up the gun.
    In the Shoot state, add one action:
    9.ChangeObjectProperty* to turn on the Visible property of W005Gun.
  9. Test again.
    The gun should now only appear when shooting.

TIPS: Switching Properties Using Animation

In this course, we used ChangeObjectProperty actions to toggle sprite visibility.
However, you can also control properties like Visible or Disabled through animations, by setting them as keyframes.

But be cautious:
If an animation is interrupted before reaching the keyframe, the change might not take effect properly.
For example, some animations like 026_Crouch use this technique to switch facial expressions via animated polygons.

For more on using AnimationPlayer, refer to the Graphics Course.


:blue_book: Next course: Part 4