Running animation not working

Hi! So I have a problem, everything works with running, I made a ‘Sprint’ named action which contains shift key, in this code I made if you press it the speed will increase to 300, everything works but even though I have an animation called ‘running’ it only plays the first frame when you press ui_right and the simple walk animation when you press ui_left. Can anyone help please?

extends CharacterBody2D

var speed = 200
var jump_velocity = -400
var direction = Vector2()
var jetpack_speed = 20
var fuel = 100
@onready var sprite_2d = $Sprite2D
	
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _running():
	speed = 300
	sprite_2d.animation = 'running'
func _physics_process(delta):
	if (velocity.x > 1 || velocity.x < -1):
		sprite_2d.animation = "walking"
	else:
		sprite_2d.animation ="default"
	if Input.is_action_pressed("Sprint"):
		_running()
	else:
		speed = 200

	if not is_on_floor():
		velocity.y += gravity * delta
		sprite_2d.animation = "jumping"
		
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = jump_velocity 

	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * speed
	else:
		velocity.x = move_toward(velocity.x, 0, 14)

	move_and_slide()
	
	if Input.is_action_just_pressed('ui_left'):
		sprite_2d.flip_h = true
	if Input.is_action_just_pressed('ui_right'):
		sprite_2d.flip_h = false

Please paste scripts between three ticks for proper formatting, you can use the </> button to make these like so

```
type or paste code here
```


Your code always plays the “walking” or the “default” animation. on top of that you are trying to always play the “running” animation when sprint is held. You need to avoid setting the animation twice in one frame.

if (velocity.x > 1 || velocity.x < -1):
    if speed > 200:
        sprite_2d.animation = "running"
    else:
        sprite_2d.animation = "walking"
1 Like

Thank you!

I’d do the logic like this, only check for the Sprint input if the character is actually moving (velocity.x is greater than 1).
Also setting the animation resets the animation, so if you set it every frame you’ll only every see the animation’s first frame, because it keeps resetting every frame, so you have to check if the animation is already what you want it to be, and only change if it’s not.

func _physics_process(delta):
	if (velocity.x > 1 || velocity.x < -1):
		if Input.is_action_pressed("Sprint"):
			speed = 300
			if sprite_2d.animation != "running":
				sprite_2d.animation = "running"
		else:
			speed = 200
			if sprite_2d.animation != "walking":
				sprite_2d.animation = "walking"
	else:
		if sprite_2d.animation != "default":
			sprite_2d.animation = "default"
1 Like

I see what you mean. It’s pretty logical. Thanks!

1 Like

It won’t reset if it’s playing the same animation forward, if your logic is sound you will not need the animation != next animation checks.

Oh you might be right I guess, the animation property or AnimatedSprite2D has this in it’s description, but I haven’t used/tested it myself.

If this value is changed, the frame counter and the frame_progress are reset.

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