Godot V4.3
So, I have spent almost 11 hours and can’t find a way to fix the player hit (attack) animation to show properly. What happens is that it does play the animation, but it is played too fast; you can barely see the arm move. I have tried Google, Copilot, and ChatGPT, and the results have been game-breaking.
Here is my code. If someone with a bit more knowledge could take a quick look at it, I would appreciate it.
`extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var deal_damage_zone: Area2D = $DealDamageZone
@onready var animation_player: AnimationPlayer = $AnimationPlayer
const SPEED = 180.0
const JUMP_VELOCITY = -300.0
var attack_type: String
var current_attack: bool
var weapon_equip: bool
var attack_damage = 10
var attack_range = 25
var attack_cooldown = 0.50
var can_attack = true
func player():
Global.playerBody = self
current_attack = false
func _process(_delta: float) → void:
if Input.is_action_just_pressed(“attack”) and can_attack:
perform_attack()
func perform_attack():
if can_attack:
can_attack = false
animated_sprite_2d.animation = “hit”
animated_sprite_2d.speed_scale = 0.5 # Adjust the speed here
# Check for enemies in the damage zone
for body in deal_damage_zone.get_overlapping_bodies():
if body.is_in_group(“enemies”):
print_debug("Enemy in range: ", body)
body.take_damage(attack_damage)
# Start a timer for the attack cooldown
var timer = Timer.new()
timer.wait_time = attack_cooldown
timer.one_shot = true
timer.connect(“timeout”, Callable(self, “_on_attack_cooldown_timeout”))
add_child(timer)
timer.start()
func _on_attack_cooldown_timeout():
can_attack = true
animated_sprite_2d.speed_scale = 1.0 # Reset the speed to normal
func _ready():
if not deal_damage_zone.is_connected(“body_entered”, Callable(self, “_on_deal_damage_zone_body_entered”)):
deal_damage_zone.connect(“body_entered”, Callable(self, “_on_deal_damage_zone_body_entered”))
if not deal_damage_zone.is_connected(“body_exited”, Callable(self, “_on_deal_damage_zone_body_exited”)):
deal_damage_zone.connect(“body_exited”, Callable(self, “_on_deal_damage_zone_body_exited”))
func _on_deal_damage_zone_body_entered(body: Node2D) → void:
print_debug("Body entered: ", body)
if body.is_in_group(“enemies”):
print_debug("Enemy detected in damage zone: ", body)
func _on_deal_damage_zone_body_exited(body: Node2D) → void:
print_debug("Body exited: ", body)
if body.is_in_group(“enemies”):
print(“Enemy left the damage zone”)
func _physics_process(delta: float) → void:
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_just_pressed("ui_up") and is_on_floor():
velocity.y = JUMP_VELOCITY
var direction := Input.get_axis("ui_left", "ui_right")
if Input.is_action_just_pressed('ui_left'):
$AnimatedSprite2D.flip_h = true
if Input.is_action_just_pressed('ui_right'):
$AnimatedSprite2D.flip_h = false
if is_on_floor():
if direction == 0:
animated_sprite_2d.animation = "idle"
else:
animated_sprite_2d.animation = "run"
else:
animated_sprite_2d.animation = "jump"
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()`