Thank you for the clarification! And it’s not a big deal no worries :]
Yeahhh so, my advice doesn’t change a whole lot. Do you see the line of code where it says
velocity.x = direction.x * speed
velocity.y = direction.z * speed
This is the very basis of your movement. That speed variable lives inside your code and is free for you to use and modify. Currently it’s value is 5.5 var speed = 5.5, and you can experiment by seeing what happens when you dial up the number to say, something like 15 or 2 or 10000 if you’re feeling wacky. (var speed = 15 #experiment by replacing this number)
You basically just want a conditional statement to check if you’re sprinting, and if so, increase that speed variable. There’s a difference between toggling and holding it, but it still largely relies on setting your project’s Input Map, so for a very simplistic example of adding a “hold to sprint” system would be tracking when a key is pressed and let go. And actually looking at your code, it’s a very very similar concept to your jumping logic, here’s how to do that:
if Input.is_action_just_pressed("sprint"):
speed = 10 #sprint speed, adjust to your liking
elif Input.is_action_just_released("sprint"):
speed = 5.5 #this is your default speed
(the example assumes you have an input mapped key linked to your ctrl key, and titled sprint, check the hyper link on Input Maps on how to do that)
Now that’s SPECIFICALLY for hold to sprint, this doesn’t toggle it, and you might find it a good exercise to figure out how to toggle it as it isn’t too many extra steps. I do recommend though that instead of having your sprint speed and walking speed values direcly set in the code, you have them as constants at the top of your script like this:
extends CharacterBody3D
const walking_speed := 5.5
const sprinting_speed := 10.0
var speed := walking_speed
## yada yada code
func _physics_process(delta):
#yada yada code im discluding for readability
if Input.is_action_just_pressed("sprint"):
speed = sprinting_speed
elif Input.is_action_just_released("sprint"):
speed = walking_speed
I recommend this because you can easily change and access these values throughout your code without having to reread or remember it. Like if you had a crouching feature that slowed your speed, its the difference between
“hmm, what’s the walking speed? let me find the code… oh its set to 5.5 during instantiation var speed = 5.5”
versus
“lets set it back using the walking_speed constant”)
and maybe later if you don’t like the walking speed being 5.5, instead of finding every time you used it, you could just change that one variable directly
Also I don’t want to overwhelm you but I recommend you use := when assigning variables. It just makes it known to the editor that it’s a float value which is useful for debugging and performance. You can learn more by looking at the Static Typing page (it also opens up autocompleting options too which makes it easier to code)
Hope that helps! Let me know if you have any more issues