Running and Jumping?

Godot Version

4.4

Question

Long story short, I’ve recently followed this tutorial on how to make a Third Person Controller in Godot for most of it. It didn’t initially have a jumping mechanic, so I improvised one. I ended up with something like this:

extends CharacterBody3D

@onready var camera_mount: Node3D = $CameraMount
@onready var animation_player: AnimationPlayer = $"Visuals/X Bot/AnimationPlayer"
@onready var visuals: Node3D = $Visuals

var SPEED = 3
const JUMP_VELOCITY = 4.5
const SMOOTH_SPEED = 10.0

var walking_speed = 3.0
var running_speed = 5.0

var running = false
var jumping = false

@export var sens_horizontal = 0.5
@export var sens_vertical = 0.5

func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _input(event):
if event is InputEventMouseMotion:
rotate_y(deg_to_rad(-event.relative.x*sens_horizontal))
visuals.rotate_y(deg_to_rad(event.relative.x*sens_horizontal))
camera_mount.rotate_x(deg_to_rad(event.relative.y*sens_vertical))

func _physics_process(delta: float) -> void:

if Input.is_action_pressed("Run"):
	SPEED = running_speed
	running = true
else:
	SPEED = walking_speed
	running = false

# Add the gravity.
if not is_on_floor():
	velocity += get_gravity() * delta

# Handle jump.
if is_on_floor():
	if Input.is_action_just_pressed("Jumping"):
		velocity.y = JUMP_VELOCITY
		jumping = true
	else:
		jumping = false
	
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("Left", "Right", "Forward", "Backward")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
var visual_dir = Vector3(input_dir.x, 0, input_dir.y).normalized()
if direction:
	if jumping:
		if animation_player.current_animation != "Jump":
			animation_player.play("Jump")
	else:
		if running:
			if animation_player.current_animation != "Running":
				animation_player.play("Running")
		else:
			if animation_player.current_animation != "Walking":
				animation_player.play("Walking")
		
	visuals.rotation.y = lerp_angle(visuals.rotation.y, atan2(-visual_dir.x, -visual_dir.z), delta * SMOOTH_SPEED)
		
	velocity.x = -direction.x * SPEED
	velocity.z = -direction.z * SPEED
else:
	if jumping:
		if animation_player.current_animation != "Jump":
			animation_player.play("Jump")
	else:
		if animation_player.current_animation != "Idle":
			animation_player.play("Idle")
		
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()

Which, tbf, works perfectly fine, but I couldn’t help but notice how I can jump mid-walk but not while running—a very basic mechanic in most third persons. I’ve tried everything to get it to work, but I still couldn’t get my character to jump while running.
Does anyone have an idea on how to make my character jump while running? I would appreciate any help atp.

I’d start debugging at the if is_on_floor() part because I wonder how quickly that turns false after pressing the jump button. Maybe the player collider is still “on the floor” for a single tick after having pressed the jump button? That would immediately set jumping back to false.

No, it didn’t. Don’t worry. The jumping part works perfectly fine in practice as I’ve tried it, and the collider isn’t “on the floor for one tick”. Now I wonder how I can make it jump while running just like it somehow does when walking, which, frankly, I’ve banged my head over for more than an hour atp…

Let’s split the problem up the way it has to be coded. Is it not playing the jump animation or is it not setting the JUMP_VELOCITY?

I’m sorry, there seems to be a slight misunderstanding here. The jump animation is playing perfectly fine. The jump animation is playing, the JUMP_VELOCITY is working, what I need help with here for the most part is not really technical, rather how to add a new mechanic that allows jumping while running because the moment you hold the key in question, the moment you start running, you can’t jump until you stop or let go of the key (walk). You can jump while idling and/or walking, but not when running. I hope that clears it up.

It’s hard to say here. I dabble in 3D and make almost exclusively 2D games so take what I say here with a grain of salt.

Input in the _physics_process() or _process() function has always been a messy implementation for me. I’d urge anything calling Input to get put into the _input() or _unhandled_input() methods. That may have something to do with it, but I don’t necessarily believe it is the problem here.

What I would do is add some type of debug print statement (or better yet adding some type of visual label) to confirm if:

  • What velocity.y is
  • Value of is_on_floor()

This will likely be more helpful, as with many “if/elif/else” statements in _process/_physics_process often get completely overwritten on the next tick. The debugging will help see what is under the hood.

As far as I can see, there doesn’t seem to be any input called for making the player walk, how are they walking?

Additionally just noticed here: If you are not pressing a button for walking (something I do not see here) then your if direction becomes false.

This means visuals.rotation.y doesn’t get called when walking, but does get called during running, and this may be causing problems. I’d try commenting this line of code out and try running again.

You see the:
var input_dir := Input.get_vector(“Left”, “Right”, “Forward”, “Backward”)
line there?
All of which trigger the walking animation instantly, as you can see in:
if direction:

if animation_player.current_animation != “Walking”:
animation_player.play(“Walking”)
unless:
if Input.is_action_pressed(“Run”) or if Input.is_action_just_pressed(“Jumping”)
is true.
That’s how the character is walking perfectly fine.

Actually, nvm. I think I may have found a way to solve this issue. Thank you anyways…

How did you fix the issue? Please share the answer here also. I’m struggling with the same thing.

Turns out, it’s my effing keyboard matrix that was terrible. All this time, it was keyboard rollover issues. The code does allow for run and jump simultaneously, but certain keyboards—like mine—don’t allow for certain key combinations and can ignore the last button (space bar in my case). I don’t know about you, but that has been the problem for me all along. If you aren’t sure, check this website out.

I also have a keyboard rollover issue it seem like. Can you tell me how you fixed this issue?

It’s an issue within the keyboard itself. Not all keyboards have good matrices, just like mine. I recommend you get a better keyboard if you can afford to get one—something like a 6KRO or NKRO keyboard will do the trick if you want to avoid ghosting/rollover entirely (or partially). And like, you can always do your own research and not take my advice at face value.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.