#Attack Animation Not Working
1 messages · Page 1 of 1 (latest)
This is the code in player.gd:
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -400.0
var attack_damage = 2
var can_attack = true
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var message_2: Label = %Message2
@onready var message_1: RichTextLabel = %Message1
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@onready var timer: Timer = $Timer
func _ready() -> void:
%Message1.position = Vector2(-62, -409)
func _process(delta: float) -> void:
pass
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_VELOCITY
var direction := Input.get_axis("move_left", "move_right")
if Input.is_action_pressed("attack_button") and can_attack:
perform_attack()
timer.start()
print("Attack!")
if direction > 0:
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
if is_on_floor():
if direction == 0:
animation_player.play("idle")
else:
animation_player.play("run")
else:
animation_player.play("jump")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func perform_attack():
can_attack = false
timer.start()
%AnimationPlayer.play("attack")
func _on_trigger_area_body_entered(_body: Node2D) -> void:
animation_player.play("fade_in")
func _on_sword_hit_area_entered(area: Area2D) -> void:
if area is HitBoxComponent:
var hitbox : HitBoxComponent = area
var attack = Attack.new()
attack.attack_damage = attack_damage
hitbox.damage(attack)
print("Hit!")
func _on_timer_timeout() -> void:
can_attack = true
Just to add a little bit of info
whenever I remove the timer or the attack cooldown, if I just hold the Left Button, my character just stops at 1st frame (of idle animation... I think?) and go back into Idle animation if I let go