How do I Make A melee attack

Hi, I’m new to coding, and I need help making a melee attack, I can’t find a good tutorial that uses Godot 4.2 so can someone tell me how to make a melee attack.

Hello, can you provide more information? What part are you stuck on? Also, the tutorial does not have to use 4.2 exactly for it to work, any Godot 4 tutorial should work.

1 Like

It’s too easy, first add a shape ray cast, size it, position it after the player, it is the range of melee attack name it hitbox so then add a timer cooldown one shot true. So now, add a variable can_attack = true, now:

if Input.is_action_pressed("melee_attack") and can_attack:
    #play here animation
    can_attack = false
    if hitbox.is_colliding():
         for i in hitbox.get_colliders():
             if i.is_in_group("Enemies"):

After cooldown timer timeout make the can_attack true

There is no specific part that im stuck on I just need to know how implement a melee attack

There are few approaches games use to handle this.

  1. Proximity based hit detection is commonly used by point-and-click rpg games meaning that when player attacks there’s a simple distance check to see whether the attack hit the target enemy and vice versa.
  2. Collision based hit detection is commonly used in games with realtime combat. General gist of these is to activate hitbox (basically Area3D/Area2D trigger) during animation to detect if hitbox enters/detects targets hurtbox.
    • In 3d games the hitbox can be attached to player weapons like swords which can be set active during attack animations.
    • In 2D its common to activate bunch of boxes matching the players attack animation(s).

These can be fairly complex to implement but that’s the general gist of how they’re commonly implemented.

thank you that helped alot

1 Like