Sprint not working

Godot Version



Why is sprinting not working?

I tried following a tutorial on how to start a first person game. when i made a way to sprint it did not work. I"m not sure if its because the video is outdated, if I missed something in the video, or if they forgot to mention something.

Here’s all the code (Its a mix of base stuff and stuff from the video)

extends CharacterBody3D

var speed = 5.0
const WALK_SPEED = 5.0
const SPRINT_SPEED = 8.0
const JUMP_VELOCITY = 4.5
const SENSITIVITY = 0.003

var gravity = 9.8

@onready var head = $head
@onready var camera = $head/Camera3D

func _ready():

func _unhandled_input(event):
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSITIVITY)
camera.rotate_x(-event.relative.y * SENSITIVITY)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(60))

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY
	# handle sprint
	if Input.is_action_just_pressed("run"):
		speed = SPRINT_SPEED
		speed = WALK_SPEED

# Get the input direction and handle the movement/deceleration.
var input_dir = Input.get_vector("left", "right", "up", "down")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
	velocity.x = direction.x * speed
	velocity.z = direction.z * speed
	velocity.x = 0.0
	velocity.z = 0.0


I don’t know if your code looks the same as what’s here, but you have your sprint code indented in the jump code. It should align with the if above it.

is_action_just_pressed the “just” pressed is only for the frame when the action is pressed, you want sprint to last more than one frame so you should use is_action_pressed instead.