![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | OgGhostJelly |
I have a player character, and everytime I press “z” or “space” it should do a sword attack animation. but the sword attack animation stops midway through the slash.
I’m pretty new to Godot so the answer is probably going to be pretty stupid and obvious so please don’t damage my ego.
extends KinematicBody2D
export var speed: = 17000.0
onready var player_sprite: AnimatedSprite = $PlayerSprite
onready var sword_sprite: Sprite = $SwordSprite
onready var animation_player: AnimationPlayer = $AnimationPlayer
var attacking = false
func _physics_process(delta: float) -> void:
var velocity = move(delta)
animation(velocity)
func move(delta: float) -> Vector2:
var direction = Vector2(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
)
direction = direction.normalized() * delta
return move_and_slide(direction * speed)
func animation(velocity) -> void:
if velocity:
player_sprite.play("walk")
animation_player.play("walk")
elif not velocity:
player_sprite.play("idle")
animation_player.stop()
if velocity.x > 0:
player_sprite.flip_h = false
sword_sprite.position.x = abs(sword_sprite.position.x)
sword_sprite.flip_h = false
elif velocity.x < 0:
player_sprite.flip_h = true
sword_sprite.position.x = -abs(sword_sprite.position.x)
sword_sprite.flip_h = true
if Input.is_action_just_pressed("attack"):
animation_player.play("attack")