Problems with "idle", "running" and "jumping" animations

Godot Version

Question

im trying to animate a player charracter in a small game im making to get the hang of it all. Im following a tutorial and i copied the code from there, but it does not work in my game.

there are 3 animations, an “idle” for standing still, a “running” for moving around and a “jumping” for when in the air.

but my player character always enters the “jumping” animation (wich is just one sprite) unless i move around, then it gets eternally stuck in the running animation. If i take out the code for the jumping animation, my player first starts in the idle animation, but when i start moving, the running animation starts and never ends.

this is the code:
#play animations
if is_on_floor():
if direction == 0:
animated_sprite.play(“idle”)
elif direction == 1:
animated_sprite.play(“run”)
elif direction == -1:
animated_sprite.play(“run”)
else:
animated_sprite.play(“jump”)

ps: first the code for when touching the floor was just an if statement for direction == 0, and an else. I added the elif’s to maybe change something but nothing did.
thanks anyone

It will help to format your code properly, press the </> button or ctrl+e in the forum to create three ticks and then paste your code between them like so

```
type or paste code here
```

Sounds like your direction variable does not ever reset to 0, how is direction defined?

i’m not sure how your direction variable works, but it seems like you expect it to be like:

  • if direction == 0, that means it’s not moving
  • direction == 1: moving right
  • direction == -1: moving left

that would mean that at all times, including while jumping, direction is supposed to be one of these values. but if the character is jumping until you move, that means it always triggers the else at the end, meaning direction starts as something that isn’t any of these values. if you use the print_debug() function in that else block and check the output tab at the bottom, you can see what that valid value is. then you can put more print_debugs in other direction-related code to see when it changes to the invalid value.

the character probably stays in the running animation because direction becomes -1/1 when you start running, but it doesn’t go back to 0 when you stop running.

as for when jumping should be played, that’s probably entirely separate to direction. i can’t be sure unless i see how the code works, but i assume what you really want is something like “if character is in the air, play the jumping animation / elif character isn’t moving, play the idle animation / elif character is moving left or right, play the walk animation”

send a link to the tutorial, i can only really guess about how the system works.

I’m having the same issue. I’m unsure if the value returns to 0. How would I fix this?
Here’s the code, and the tutorial video I’m following:

extends CharacterBody2D

const SPEED = 130.0
const JUMP_VELOCITY = -300.0

Get the gravity from the project settings to be synced with Rigidbody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

@onready var animated_sprite = $AnimatedSprite2D

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

# Handle jump. I replaced "ui_accept" with my own keybindings.
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY

#Get the input direction: -1, 0, 1
var direction = Input.get_axis("move_left", "move_right")


# Flip da Sprite. 
if direction > 0:
	animated_sprite.flip_h = false
elif direction < 0:
	animated_sprite.flip_h = true
	
	# Play animation
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("idle")
		else: 
			animated_sprite.play("run")
	else:
		animated_sprite.play("jump")
# Apply movement
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()