Sprint animation cancelled at first frame by the walk animation.

I want it to be where if I press shift and move it plays the sprint animation, but whatever I do either it plays only the first frame, or flashes it for a second, or breaks every other animation like the jump and the idle.

Here’s the code.

extends CharacterBody2D

var SPEED
const WALK_SPEED = 300.0
const RUN_SPEED = 1000;
const JUMP_VELOCITY = -700.0

@onready var animated_sprite = $AnimatedSprite2D

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
	SPEED=WALK_SPEED;
	if Input.is_action_pressed("Run"):
		SPEED=RUN_SPEED
	else:
		SPEED=WALK_SPEED
	
	# 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.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction := Input.get_axis("Go_left", "Go_right")
	
	if direction > 0:
		$AnimatedSprite2D.flip_h = false
	elif direction < 0:
		$AnimatedSprite2D.flip_h = true
	
	if is_on_floor():
		if direction == 0:
			$AnimatedSprite2D.play("idle")
		else:
			$AnimatedSprite2D.play("walk")
	else:
		$AnimatedSprite2D.play("jump")
	if Input.is_action_just_pressed("Run"):
		$AnimatedSprite2D.play("sprint")
	
	
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	move_and_slide()

You’re setting the animation to “walk” every frame.

I know I just don’t know how to fix it without messing the idle animation or jump animation.

Haven’t tried to run this code, but it should work.

Consider refactoring your code as the many different states and if-statements will eventually make your code impossible to read and understand quickly.

1 Like

I tried it and sadly did not work it got a bunch of errors.

Please post these errors and include where they happen.

e: it appears the website ate my indentation. Strange, as the preview window didn’t show anything strange. I edited the code; please try again.

1 Like

It worked! thank you so much!!