Help with attack animation playing for only one frame

Godot Version

4.2.1

Im currently using this: ( terribly sorry for plain text, I’m not sure how to format it)

func _input(event):
if event.is_action_pressed(“Attack”):
doAttack()

func doAttack():
animation.play(“Attack_Sides”)

The animation only plays for 1 frame when i press the key “Attack” in my case the space key. What would be the way to do this?

Thank you in advance, I am new to godot and this forum.

Double check if it plays correctly in the animation player, if not you need to fix it there

It plays correctly in the animation player. It is short tho. I just tried making it longer but the problems is still there.

is that the whole code file you posted?

No just the part i use for attack animation (no collision detection is yet in place). I just tried replacing the attack animation with one of the running animation and it works fine in that case. Here is the whole script:

extends CharacterBody2D

@onready var animation = $AnimationPlayer
@onready var sprite_2d = $Sprite2D

var speed = 400 # speed in pixels/sec

var idelAnimation = “Idel_Down” #for knowing which idel animation to play depending on direction
var isLeft = false #for knowing if to flip sprite

func _input(event):
if event.is_action_pressed(“Attack”):
doAttack()

func doAttack():
animation.play(“Attack_Sides”)

func _physics_process(delta):
# Animations :
var direction = Input.get_vector(“Left”, “Right”, “Up”, “Down”)
velocity = direction * speed

if velocity.x != 0 and velocity.y == 0:
	animation.play("Run_Sides")
	idelAnimation = "Idel_Sides"
	isLeft = velocity.x < 0
elif velocity.y < 0:
	animation.play("Run_Up")
	idelAnimation = "Idel_Up"
elif velocity.y > 0:
	animation.play("Run_Down")
	idelAnimation = "Idel_Down"
else:
	animation.play(idelAnimation)

sprite_2d.flip_h = isLeft

# end of animations


move_and_slide()

looking at if velocity.x != 0 and velocity.y == 0: here. Maybe it starts playing but gets overwritten by the other animations? “Run_Sides” would overwrite it if you weren’t moving. Try commenting those lines out for now and see if it works

I just commented out all other animation line apart from the attack ones. Now it does start playing, but doesn’t stop. Thank you for all your help btw hope this isn’t bothersome for you. What would be the best way to do a attack animation together with other animations, is there a good simple example somewhere?

I’ll Look. Also, I think animation looping is on by default (I do 3D) so look for that

Look around for open source 2D projects like platformer demos, Those should have the cod for all of that. The way I would do it would probably be a bad one.

Thank you for all your help! I managed to fix it now! Here is the final script, it may not be the most perfect or optimized but heyyyy.

extends CharacterBody2D

@onready var animation = $AnimationPlayer
@onready var sprite_2d = $Sprite2D

var speed = 400 # speed in pixels/sec

var idelAnimation = “Idel_Down” #for knowing which idel animation to play depending on direction
var isLeft = false #for knowing if to flip sprite
var isAttacking = false

func _input(event):
if event.is_action_pressed(“Attack”):
doAttack()

func doAttack():
isAttacking = true
animation.current_animation = “Attack_Sides”

func _physics_process(delta):
#Animations :
var direction = Input.get_vector(“Left”, “Right”, “Up”, “Down”)

if !isAttacking:
	velocity = direction * speed 
	if velocity.x != 0 and velocity.y == 0:
		animation.play("Run_Sides")
		idelAnimation = "Idel_Sides"
		isLeft = velocity.x < 0
	elif velocity.y < 0:
		animation.play("Run_Up")
		idelAnimation = "Idel_Up"
	elif velocity.y > 0:
		animation.play("Run_Down")
		idelAnimation = "Idel_Down"
	elif Input.is_anything_pressed()==false:
		animation.play(idelAnimation)
else:
	velocity = direction * 0

sprite_2d.flip_h = isLeft

# end of animations


move_and_slide()

func _on_animation_player_animation_finished(anim_name):
if anim_name == “Attack_Sides”:
isAttacking = false

No problem! btw for code formatting there’s either the icon above the text editor (</>) or you can just use pastebin for bigger scripts. If you do use pastebin turn on syntax highlighting and select GDscript. Good luck with your game!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.