Check the movement in AnimationPlayer. Try playing the 033_Swing animation. If something feels off, make minor adjustments in the properties of W001Sword.
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.
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.
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.
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.
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.
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.
Let’s try a test play. If set correctly, the sword should only be visible during sword attacks.
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.
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.