Tutorial: Script Course #3 – Creating a Sword Attack

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