Hi, I’m using Godot 4. This is my movement code:
extends CharacterBody2D
@export var walk_speed = 150.0
@export var run_speed = 250.0
@export_range(0, 1) var acceleration = 0.1
@export_range(0, 1) var decelaration = 0.1
const JUMP_VELOCITY = -400.0
@onready var animated_sprite = $AnimatedSprite2D
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
var speed
if Input.is_action_pressed("run"):
speed = run_speed
else:
speed = walk_speed
# 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("left", "right")
if direction:
velocity.x = move_toward(velocity.x,direction * speed,speed * acceleration)
animated_sprite.play("walk")
animated_sprite.flip_h = direction < 0
else:
velocity.x = move_toward(velocity.x, 0, walk_speed * decelaration)
animated_sprite.play("Idle")
move_and_slide()
but the problem I have is that I can’t write a code that makes the run animation play when the player holds down the run button. please help!