Animation shows only first Frame

Godot Version

4.2.1

Question

Hello Community,
to Time, i learn gdscript and Godot new. In some thing´s i found a own Solution, but now i need some help.

My Code:

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

@onready var player = $AnimatedSprite2D

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var level_bounds = Rect2(0, 0, 1024, 768) # Beispiel: Spielfeldgröße (anpassen an deine Szene)

func _physics_process(delta):
	player.play("Idle")
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("Jump") and is_on_floor():
		if Input.is_action_just_pressed("Left"):
			player.play("jump-l")
		elif Input.is_action_just_pressed("Right"):
			player.play("jump-r")
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("Left", "Right")
	if direction:
		if direction < 0: # Negative direction means Left
			player.play("walk-l")
		elif direction > 0: # Positive direction means Right
			player.play("walk-r")
		velocity.x = direction * SPEED
	else:
		player.play("Idle")
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

	# Check if player has fallen out of bounds
	if position.y > level_bounds.position.y + level_bounds.size.y:
		on_player_fallen()

func on_player_fallen():
	position.x = 540
	position.y = 300
	pass

when i go left or right, he give the first image of the Animation walk-l or walk-r, but dont play the full Animation. I have uploaded a Video to show the Problem: https://youtu.be/B4VALgp9QAE

You are playing the Idle animation at the beginning of the function. It starts playing the walk animation for 1 frame but the next frame it starts playing the Idle then the walk animation again and again. You’ll need to fix your animation logic.

2 Likes

Calling player.play(animation) on every frame is wrong. Create AnimationTree, where you can define transitions and conditions. Then you would simply set the conditions in the _physics_process function.
Check out this video. It’s for 3D, but the principles are the same.

2 Likes

Thanks to you two. I will fix the Problem and will read the AnimationTree Docu

1 Like

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