How to add 2d attack

Godot Version

Mew mew 4.7 last version

Question

How to add 2d attack system I’m a beginner and I only know a little. When I got to the part about enemies and attacks, I got lost and couldn’t find a way. I want a way out, and this is my game’s problem. Thank you all.

extends CharacterBody2D
class_name player
const SPEED = 300.0
const JUMP_VELOCITY = -400.0


@onready var animation_player = $AnimationPlayer
@onready var sprite_2d = $Sprite2D


func _physics_process(delta):
	
	
		
	if not is_on_floor():
		velocity += get_gravity() * delta

	
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		
		animation_player.play("jump2")

	
	var direction := Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
		
		sprite_2d.flip_h = direction < 0
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	
	
	update_animations(direction)

func update_animations(directio
	if is_on_floor() :
		
		if direction == 0:
			animation_player.play("idle")
		else:
			animation_player.play("run")
	else:
		if velocity.y > 0 and animation_player.has_animation("fall"):
			animation_player.play("fall") 
func respawn():
 
	if Gamemanagar.last_checkpoint_position != Vector2.ZERO:
		global_position = Gamemanagar.last_checkpoint_position
	else:
		
		get_tree().reload_current_scene()
		
	velocity = Vector2.ZERO 

You’re almost there already.

You already detect an input, play an animation and do something with the velocity with your jump. So now, detect a different input and play an attack animation and do something else with the velocity if you want.

Once you have that going you can detect if you hit anything and apply some damage, but you can get to that later, or come back here if you need any help with that too.

Can you write my commands, some with the addition of a command to run the animation of the strike?

Yes—start by getting the attack animation to play completely before adding damage.

Add an attack action in Project Settings → Input Map, bind a key, and create a non-looping animation named attack in your AnimationPlayer.

Remove animation_player.play("jump2") from the jump-input block, but keep the jump velocity assignment. Then replace your incomplete update_animations function with:

func update_animations(direction):
if animation_player.current_animation == “attack” and animation_player.is_playing():
return

if Input.is_action_just_pressed("attack"):
    animation_player.play("attack")
    return

if not is_on_floor():
    if velocity.y < 0:
        animation_player.play("jump2")
    elif animation_player.has_animation("fall"):
        animation_player.play("fall")
elif direction != 0:
    animation_player.play("run")
else:
    animation_player.play("idle")

Keep your existing update_animations(direction) call after move_and_slide().

The first check protects the attack from being replaced by idle/run/fall on the next frame. Once the animation finishes, normal animations resume. Movement still works during the attack; this only controls animation, not damage.

Thanks you i try this