#Player attack won't effect enemies

10 messages · Page 1 of 1 (latest)

solar tinsel
midnight kestrelBOT
#
Embedding code in discord messages
Inline Code

When you surround some words with single backticks like `this`, it will be formatted as code.

Code Blocks

You can also include code blocks by surrounding the code with three backticks. If you add "swift" then you will also get basic syntax highlighting:
```swift
print("hello world")
```
Discord will then show it as a code block like this:

print("hello world")
Upload

If your code snippet is rather long, you can upload it to a text paste site. One option that supports syntax highlighting for GDScript is https://bpa.st/

austere siren
#

Show enemy and player code if you want us to have any idea what could be wrong

solar tinsel
#

Player:
`extends CharacterBody2D

@export var speed: float = 100.0
@export var max_health: int = 100
@export var attack_damage: int = 20
@export var attack_cooldown: float = 0.5

var health: int = max_health
var direction: Vector2 = Vector2.ZERO
var last_direction: Vector2 = Vector2.DOWN
var can_attack: bool = true
var is_attacking: bool = false

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var attack_area: Area2D = $AttackArea

func _ready():
add_to_group("Player")

func _physics_process(delta: float) -> void:
if is_dead():
return

handle_input()
move_and_slide()

func handle_input() -> void:
if not is_attacking:
handle_movement()

if Input.is_action_just_pressed("attack") and can_attack:
    perform_attack()

func handle_movement() -> void:
direction = Vector2(
Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"),
Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
).normalized()

if direction != Vector2.ZERO:
    velocity = direction * speed
    last_direction = direction
    play_walk_animation()
else:
    velocity = Vector2.ZERO
    play_idle_animation()

func perform_attack() -> void:
can_attack = false
is_attacking = true
play_attack_animation()

await get_tree().create_timer(0.1).timeout

for body in attack_area.get_overlapping_bodies():
    if body.is_in_group("Enemies") and body.has_method("take_damage"):
        body.take_damage(attack_damage)

await sprite.animation_finished
is_attacking = false

await get_tree().create_timer(attack_cooldown).timeout
can_attack = true

func take_damage(amount: int):
health -= amount
print("Player took damage! Health:", health)
if health <= 0:
die()

func die():
print("Player died.")
queue_free()

func is_dead() -> bool:
return health <= 0

func play_walk_animation():
if abs(last_direction.x) > abs(last_direction.y):
sprite.animation = "walk_sides"
sprite.flip_h = last_direction.x < 0
elif last_direction.y < 0:
sprite.animation = "walk_up"
else:
sprite.animation = "walk_down"
sprite.flip_h = last_direction.x < 0
sprite.play()

func play_idle_animation():
if abs(last_direction.x) > abs(last_direction.y):
sprite.animation = "Idle_sides"
sprite.flip_h = last_direction.x < 0
elif last_direction.y < 0:
sprite.animation = "Idle_up"
else:
sprite.animation = "Idle_down"
sprite.flip_h = last_direction.x < 0
sprite.play()

func play_attack_animation():
if abs(last_direction.x) > abs(last_direction.y):
sprite.animation = "attack_sides"
sprite.flip_h = last_direction.x < 0
elif last_direction.y < 0:
sprite.animation = "attack_up"
else:
sprite.animation = "attack_down"
sprite.flip_h = last_direction.x < 0
sprite.play()

func _on_Players_hitbox_body_entered(body: Node2D) -> void:
if body.is_in_group("Enemies") and body.has_method("get_attack_damage"):
take_damage(body.get_attack_damage())`

#

Slime:
`extends CharacterBody2D

@export var move_speed: float = 50
@export var attack_range: float = 20.0
@export var attack_cooldown: float = 1.0
@export var damage: int = 10
@export var max_health: int = 100

var health: int = 0
var player: Node2D = null
var can_attack: bool = true
var is_dead: bool = false

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var hitbox: Area2D = $AnimatedSprite2D/Hitbox

func _ready() -> void:
health = max_health
add_to_group("Enemies")

func _physics_process(delta: float) -> void:
if is_dead:
return

if player:
    var distance = global_position.distance_to(player.global_position)

    if distance <= attack_range:
        velocity = Vector2.ZERO
        play_idle_animation()
        if can_attack:
            attack()
    else:
        var direction = (player.global_position - global_position).normalized()
        velocity = direction * move_speed
        play_move_animation(direction)

    move_and_slide()
else:
    velocity = Vector2.ZERO
    play_idle_animation()

func attack() -> void:
can_attack = false
print("Slime attacking player!")

if player and global_position.distance_to(player.global_position) <= attack_range:
    if player.has_method("take_damage"):
        player.take_damage(damage)

await get_tree().create_timer(attack_cooldown).timeout
can_attack = true

func take_damage(amount: int) -> void:
if is_dead:
return

health -= amount
print("Slime took damage! Health:", health)

if health <= 0:
    die()

func die() -> void:
is_dead = true
print("Slime died.")
sprite.play("death")
await sprite.animation_finished
queue_free()

func play_move_animation(direction: Vector2) -> void:
if abs(direction.x) > abs(direction.y):
sprite.play("jump_sides")
sprite.flip_h = direction.x < 0
else:
sprite.flip_h = false
if direction.y > 0:
sprite.play("jump_front")
else:
sprite.play("jump_back")

func play_idle_animation() -> void:
sprite.flip_h = false
sprite.play("idle_front")

func _on_detection_area_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
player = body
print("Player found:", body.name)

func _on_detection_area_body_exited(body: Node2D) -> void:
if body == player:
player = null
print("Player left area")

func _on_enemy_hitbox_body_entered(body: Node2D) -> void:
if body.name == "AttackArea":
var player_node = body.get_parent()
if player_node.is_in_group("Player") and player_node.has("attack_damage"):
take_damage(player_node.attack_damage)
else:
take_damage(20)`

#

those are the 2

fallen widget
#

Do you know how to use breakpoints? Best way to debug this is to throw breakpoints on code that should be hit on the way to modifying the enemy.

Is if body.is_in_group("Enemies") being hit? If not, at least you can refine your question to that.

#

Your enemy node has an alert by it. I'm guessing it says you didn't give it a collision shape as a direct child. That would prevent detecting its body.

solar tinsel
#

`extends CharacterBody2D

@export var speed: float = 100.0
@export var max_health: int = 100
@export var attack_damage: int = 20
@export var attack_cooldown: float = 0.5

var health: int = max_health
var direction: Vector2 = Vector2.ZERO
var last_direction: Vector2 = Vector2.DOWN
var can_attack: bool = true
var is_attacking: bool = false

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var attack_area: Area2D = $AttackArea

func _ready():
add_to_group("Player")
attack_area.connect("body_entered", Callable(self, "_on_attack_area_body_entered"))

func _physics_process(delta: float) -> void:
if is_dead():
return

handle_input()
move_and_slide()

func handle_input() -> void:
if not is_attacking:
handle_movement()

if Input.is_action_just_pressed("attack") and can_attack:
    perform_attack()

func handle_movement() -> void:
direction = Vector2(
Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"),
Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
).normalized()

if direction != Vector2.ZERO:
    velocity = direction * speed
    last_direction = direction
    play_walk_animation()
else:
    velocity = Vector2.ZERO
    play_idle_animation()

func perform_attack() -> void:
can_attack = false
is_attacking = true
play_attack_animation()

attack_area.monitoring = true

await get_tree().create_timer(0.2).timeout

attack_area.monitoring = false

await sprite.animation_finished
is_attacking = false

await get_tree().create_timer(attack_cooldown).timeout
can_attack = true

func _on_attack_area_body_entered(body: Node) -> void:
print("Body entered attack area:", body.name)

if body.is_in_group("Enemies"):
    print("Body is in 'Enemies' group")
    if body.has_method("take_damage"):
        print("Body has 'take_damage' method")
        breakpoint
        body.take_damage(attack_damage)
    else:
        print("Body is missing 'take_damage' method")
else:
    print("Body is not in 'Enemies' group")

func take_damage(amount: int):
health -= amount
print("Player took damage! Health:", health)
if health <= 0:
die()

func die():
print("Player died.")
queue_free()

func is_dead() -> bool:
return health <= 0

func play_walk_animation():
if abs(last_direction.x) > abs(last_direction.y):
sprite.animation = "walk_sides"
sprite.flip_h = last_direction.x < 0
elif last_direction.y < 0:
sprite.animation = "walk_up"
else:
sprite.animation = "walk_down"
sprite.flip_h = last_direction.x < 0
sprite.play()

func play_idle_animation():
if abs(last_direction.x) > abs(last_direction.y):
sprite.animation = "Idle_sides"
sprite.flip_h = last_direction.x < 0
elif last_direction.y < 0:
sprite.animation = "Idle_up"
else:
sprite.animation = "Idle_down"
sprite.flip_h = last_direction.x < 0
sprite.play()

func play_attack_animation():
if abs(last_direction.x) > abs(last_direction.y):
sprite.animation = "attack_sides"
sprite.flip_h = last_direction.x < 0
elif last_direction.y < 0:
sprite.animation = "attack_up"
else:
sprite.animation = "attack_down"
sprite.flip_h = last_direction.x < 0
sprite.play()

func _on_Players_hitbox_body_entered(body: Node2D) -> void:
if body.is_in_group("Enemies") and body.has_method("get_attack_damage"):
take_damage(body.get_attack_damage())`

#

it says that the body isn't in the Enemies group even though it is