My run animation is only playing the first frame

Godot Version

<Godot 4.2.2>

Hi guys,

im new to Godot and im trying to make a 2D Platformer game. Until now everything worked fine my player can walk, jump and idle he also plays the right animations for everyone of these actions. Now my problem when i press “run” while my player walks he gets faster as i intended but he is stuck on the first frame of his run animation. This frame will be there until i let go off the “run” button. I really dont understand why my run animation wont play fully i hope someone here can help me Thanks in advance

P.S. sorry for my poor english it is not my native language

Here is my Code:

extends CharacterBody2D

@onready var animated_sprite = $AnimatedSprite2D

@export var SPEED : int = 155
@export var RUN_SPEED : int = 240
@export var GRAVITY : int = 900
@export var JUMP_FORCE : int = 255

func _physics_process(delta):

var direction = Input.get_axis("move_left", "move_right")

if direction:
	
	velocity.x = direction * SPEED
	if is_on_floor():
		animated_sprite.play("walk")
		if Input.is_action_pressed("run") and direction != 0:
			animated_sprite.play("run")
			velocity.x = direction * RUN_SPEED
	
else:
	velocity.x = 0
	
	if is_on_floor():
		animated_sprite.play("idle")
		

	
# Flip Sprite

if direction == 1:
	animated_sprite.flip_h = false
elif direction == -1:
	animated_sprite.flip_h = true

# Gravity

if not is_on_floor():
	
	velocity.y += GRAVITY * delta
	
# Jump

if Input.is_action_just_pressed("jump") and is_on_floor():
	
	velocity.y -= JUMP_FORCE
	
	animated_sprite.play("jump")
		


move_and_slide()

Did you have turn on the loop in that run animation

i just checked and it wasnt turned on but the problem remains

You are playing the walk and the run animations in the same frame. Here is what happens:

  • You determine the player is on the floor
  • You play the walk animation
  • You determine the “run” action is pressed
  • You play the run animation

So each frame, the animation resets and plays run from the start. You need to separate those two things!

Normally if you call .play("walk") and the animation player is already playing “walk”, it doesn’t reset, it just continues. But when you switch to “walk” and then to “run” each frame, it starts from the start every time

My guess is you want this:

if is_on_floor():
	if Input.is_action_pressed("run") and direction != 0:
		animated_sprite.play("run")
		velocity.x = direction * RUN_SPEED
	else:
		animated_sprite.play("walk")
1 Like

Thanks for the reply!

I now undertand what i was doing wrong! It finally works thank you so much.

1 Like

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