Sprite Moving Animation Issue

Godot Version

4.6.2

Question

So I have this issue that when I run the code, the player animation is always run even when I am not moving it, it’s supposed to be the idle animation, I followed this youtube tutorial called platformer tutorial made by Coding With Russ, pls help and don’t change up the code too much.

extends CharacterBody2D


const SPEED = 350.0
const JUMP_VELOCITY = -600.0
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D


func _physics_process(delta: float) -> void:
	if velocity.x>1 or velocity.x<1:
		animated_sprite_2d.animation='run'
	else:
		animated_sprite_2d.animation='idle'
	if not is_on_floor():
		velocity += get_gravity() * delta
		animated_sprite_2d.animation='jump'
	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	var direction := Input.get_axis("left", "right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	if direction==1:
		animated_sprite_2d.flip_h=false
	if direction==-1:
		animated_sprite_2d.flip_h=true

It should probably check if velocity.x is larger than 1 or lower than -1:

	if velocity.x > 1 or velocity.x < -1:

(Which could be simplified as:)

	if abs(velocity.x) > 1:
1 Like

It worked, thanks!

2 Likes