How to add a run animation?

Godot Version

4.4

Question

I have a script for my player but I can’t figure out how to make the run animation play when the player is holding the run button. I tried to make it so that once the player is over a certain speed the animation plays but that didn’t work :(.

extends CharacterBody2D

var walk_speed = 130
var run_speed = 200
var gravity = 15
var jump_force = 400
@onready var ap = $AnimationPlayer
@onready var sprite = $Sprite2D
func _physics_process(delta):
if !is_on_floor():
velocity.y += gravity
if velocity.y > 1000:
velocity.y = 1000
var speed
if Input.is_action_pressed(“run”):
speed = run_speed
else:
speed = walk_speed

if Input.is_action_just_pressed("jump"):
	velocity.y = -jump_force

var horizontal_direction = Input.get_axis("left","right")

velocity.x = speed * horizontal_direction 
if horizontal_direction != 0:
	sprite.flip_h = (horizontal_direction == -1)
move_and_slide()
update_animations(horizontal_direction)

func update_animations(horizontal_direction):
if is_on_floor():
if horizontal_direction == 0:
ap.play(“Idle”)
else:
ap.play(“Walk”)
else:
if velocity.y < 0:
ap.play(“Jump”)
elif velocity.y > 0:
ap.play(“Fall”)

Any help is greatly appreciated.

Make sure to format your paste between three ticks ```


What have you tried so far? It doesn’t seem like there is a call to ap.play("Run") in this code

My bad for not formating the paste, I don’t use the forums often and I don’t know the ins and outs yet.

Also you’re right, the ap.play(“Run”) isn’t called because I wrote some come earlier for it but it wasn’t working so I removed it but it was something like:

if velocity.x > 1:
   ap.play("Run")
elif velocity.x < 1:
   ap.play("Walk")

With 1 being the walking speed but I didn’t know how to get the walking speed in the first place so I gave up on trying to make this work, and besides when I ran it only worked half the time.

I also tried to simply tack on the Run animation whenever the run button is pressed but as you can imagine that failed also (the animation just bugged out).

I think your run speed is 200, though you could check if velocity.x > walk_speed:.

You should be able to track when the run button is pressed as well, it sounds like a good, consistent way to do it

if horizontal_direction == 0:
    ap.play("Idle")
else:
    if Input.is_action_pressed("run"):
        ap.play("Run")
    else:
        ap.play("Walk")
1 Like

Well I tried what you suggested but it looks like the run animation starts playing for a fraction of a second but the immidiatlety reverts to the walking animation, I checked to see if I might have messed up the animation itself like not setting it to loop but it all looks correct.

func update_animations(horizontal_direction):
	if is_on_floor():
		if horizontal_direction == 0:
			ap.play("Idle")
		else:
			if Input.is_action_just_pressed("run"):
				ap.play("Run")
			else:
				ap.play("Walk")
	else:
		if velocity.y < 0:
			ap.play("Jump")
		elif velocity.y > 0:
			ap.play("Fall")

Input.is_action_just_pressed only triggers for the one frame the action has just been pressed on, try Input.is_action_pressed

1 Like

Omg I must’ve misclicked when typing, it works now! Thank you very much for your help.

1 Like