How to make an attack in Godot 2d?

Godot Version

I’m using Godot 4.5

Question:

I’m a beginner and finally decided to add simple combat to my game, but I don’t know how. I’ve tried making functions but I don’t know how to link them together or if I need to do something else. I’m quite lost and most videos don’t have what I need. This is my script, please help me with my combat system.

My player also uses an animation player with a sprite 2D for the animation with keys for the hitbox(area 2D w/ CollisionShape2D).

extends CharacterBody2D

@export var SPEED := 100
const jump_force = -350
@export var Health := 3
@export var damage := 1

var is_attacking := false

@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var hitbox: Area2D = $Sprite2D/Hitbox
@onready var collision_shape: CollisionShape2D = $Sprite2D/Hitbox/CollisionShape2D


func _ready() -> void:
	collision_shape.disabled = true

func _physics_process(delta: float) -> void:
	
	if not is_on_floor():
		velocity += get_gravity() * delta
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_force
	var direction := Input.get_axis("move_left","move_right")
	if direction:
		velocity.x = SPEED * direction
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	move_and_slide()
	
	
	#animation
	if !is_attacking: 
		if is_on_floor() :
			if velocity.x == 0:
				animation_player.play("Idle")
			else: animation_player.play("Walk")
		else: animation_player.play("Jump")
	
	if direction < 0:
		sprite_2d.scale.x = -1
	elif direction > 0: sprite_2d.scale.x = 1
	
	if Input.is_action_just_pressed("attack"):
		startattack()

func take_damage(int, damage): 
	Health -= damage
	print("I got hit")
	print(Health)


func _on_swordhit_area_entered(area: Area2D) -> void:
	if area.is_in_group("enemy"):
		print("I hit something!!!")
		area.take_damage(damage)

func startattack():
	is_attacking = true
	animation_player.play("Attack")
	await animation_player.animation_finished
	is_attacking = false

hey!

please put the code on the preformatted text(you can toggle that by Ctrl+E) ,it’s quite hard to read the code.

Thanks, I didn’t know how to fix that

It’s still not showing properly, the indentation is all over the place and some keywords are in the wrong places.
Please fix it by copying the code from the Godot editor and pasting it again in your post between the three backticks ```

I updated it, sry this is my first post

1 Like

Now it’s much better.
And what exactly doesn’t work in here? You have a good foundation built already.

I don’t know how to make the player and slime enemy communicate so that when the player attacks (and the hitbox turns on) the slime takes damage and for the player to take damage when he runs into the slime.

here is my slime code btw

extends Node2D

@export var Health := 2

func _ready() -> void:
	add_to_group("enemy")

func take_damage(int, damage):
	Health -= damage
	print("Slime" + str(Health))
	if Health >= 0:
		print("slime died")
		queue_free()


I see you already have a Hitbox component on your player. This is the component that should receive the damage. All you need is to have a Hurtbox component, which deals the damage. Here’s an example tutorial I found just by googling “Godot Hitbox Hurtbox”, maybe you can try it out.

Thanks, I’ll try it out