Hello!
I am making a top-down game using GDscript where you can move (obviously.) I have actions set for up, down, etc. I made animations for the movement but just can’t get them to work. I don’t want an idle animation (at least not yet), and for some reason the animationplayer only plays ONE FRAME. Here is my code if anyone needs it:
extends CharacterBody2D
Sorry I took so long to reply. Again, I MIGHT be putting it in the wrong spot. I’m new to coding in GDscript, so I might have a mishap. Here’s the code:
if Input.is_action_pressed(“down”):
velocity.y += speed
$Player/Animate.play(“WalkDown”)
else:
$Player/Animate.stop()
if !$Player/Animate.is_playing():
$Player/Animate.play(“WalkDown”)
That’s same code to OP. The full .gd script would make the problem clear, and whether there is any other class/code which uses $Player/Animate.
If you want to get it solved 100%, upload the model+animation or the full project (MRP)
That said, to debug this without the above and confirm whether the problem is in the animation or the code, try this:
Comment everything away, and on the function func _ready(), place this codeline: $Player/Animate.play(WalkDown")
Agree with @theyellowarchitect , if you could post the full script plus your overall setup, it would help us help you.
In the snipped you posted, it seems like you put the condition check if Animate is playing in the wrong spot: It’s currently checking this condition as long as “down” is not pressed.
Try it like this. I’ve added some comment to make it clear how the code is executed. If you’re very new to programming, I’d suggest doing a basic course from Youtube creating a small game so you can familiarize yourself with it.
if Input.is_action_pressed(“down”): #What happens when down is pressed.
velocity.y += speed
if !$Player/Animate.is_playing(): #Checks if the Animationplayer is playing an animation. If not:
$Player/Animate.play(“WalkDown”) #Animationplayer starts WalkDown animation.
else: #What happens if "down" is not pressed.
$Player/Animate.stop() #Stops any animation that Animationplayer is playing.
Since you have separate if statements for each direction instead of a single if-elif one, all of them are being ran. So if you are pressing left, down, or up but not right, the animation will stop because the last else block is ran.
if Input.is_action_pressed("down"): #What happens when down is pressed.
velocity.y += speed
if !$Player/Animate.is_playing(): #Checks if the Animationplayer is playing an animation. If not:
$Player/Animate.play("WalkDown") #Animationplayer starts WalkDown animation.
else: #What happens if "down" is not pressed.
$Player/Animate.stop() #Stops any animation that Animationplayer is playing.
if Input.is_action_pressed("up"):
velocity.y -= speed
$Player/Animate.play("WalkUp")
else:
$Player/Animate.stop()
if Input.is_action_pressed("left"):
velocity.x -= speed
$Player/Animate.play("WalkLeft")
if Input.is_action_pressed("right"):
velocity.x += speed
$Player/Animate.play("WalkRight")
else:
$Player/Animate.stop()
move_and_slide()
I am using a spritesheet that is 4 by 4 for the animation. I don’t want to post the file because I dont want to show much. It still isn’t working, but thank you!