Melee attacks

Problem

You want to implement a melee attack, such as a sword or punch.

Solution

For this example, we’ll assume we have already set up a character with one or more attack animations. To illustrate, we’ll use these two attacks:

alt

alt

We can detect the sword hitting the target using an Area2D, but we only want that area to be active during the swing. In order for this activation to be in sync with the animation, we’ll use the AnimationPlayer to control it.

Add an Area2D and CollisionShape2D to the scene. We’ll use a rectangle shape for the hitbox and size it so that it covers the sword during the swing frame.

alt

Move the animation to the first frame and check the Disabled property of the area. Click the keyframe icon to add a track to the animation. Then advance the animation to the frame where the sword is extended, and add another keyframe with Disabled unchecked. Finally, advance to the end of the swing and keyframe Disabled on once more.

alt

Now connect this new area’s area_entered signal (or, depending on how your game is set up, body_entered). For the purposes of this demo, let’s assume that any body that can take damage has an Area2D defined and placed in a group called “hurtbox”.

func _on_SwordHit_area_entered(area):
    if area.is_in_group("hurtbox"):
        area.take_damage()

Now you should be able to try it out and see the attack doing damage if the target is inside the sword’s hitbox.

alt

Changing the hitbox size

When you have more than one attack animation, the size of the affected area may not be the same. In the above attack animations, the first one is an upward sweeping attack that covers more area. To handle this, we also need to add an animation track for the collision shape’s Extents property. Set this and keyframe it at the start of each animation.

alt

alt

Like video?

Comments

comments powered by Disqus