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”)
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).
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")