Character is supposed to double jump, but game lets you jump 3 times

Godot Version

V4.3

Question

I was coding a double jump and it lets you jump 3 times

var jump_count = 0
var max_jumps = 2

If Input.is_action_just_pressed(“jump”) and jump_count < max_jumps:
Jump_count += 1
velocity.y = JUMP_VELOCITY
print(jump_count)

When I press jump 3 times, it prints:

1
1
2

Why does 1 appear twice?

paste your entire player script, and format is using the </> icon when editing a post.
Because I can’t tell if something else is interfering with it.
Only thing I can see right now is maybe changing the and to an or.

If it isn’t there, surround you code with

tickticktick
the code
tickticktick

Where every thick is equal to this: `
You can copy and paste it.

It should look like:

oh, look code!
1 Like

your counter starts counting at 0. you need to add 1 3 times to get it to 2. try setting jump_count to 1

Here’s my script, I was able to fix it by just changing max_jumps to 1 but I’m just confused why this is happening

extends CharacterBody2D


const SPEED = 150.0
const JUMP_VELOCITY = -250.0

var jump_count = 0
var max_jumps = 1
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(_delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * _delta
	
	# Handle jump and double jump.
	if Input.is_action_just_pressed("jump") and max_jumps > jump_count:
		jump_count += 1
		print(jump_count)
		velocity.y = JUMP_VELOCITY
		
	
	if is_on_floor():
		jump_count = 0
	
	# get direction
	var direction = Input.get_axis("move_left", "move_right")
	
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true
	
	# play animations
	if is_on_floor():
		if direction != 0:
			animated_sprite.play("run")
		else:
			animated_sprite.play("idle")
	
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

1 Like

The problem is that you’re setting jump_count to 0 after jumping. When you press the jump button, velocity changes, but the character hasn’t left the ground, so is_on_floor() returns true.

1 Like

You might be able to do what grulps said by adding a “reset_jump_count” variable.

Once you jump set it to false immediately before any of the jumping code.
In the is_on_floor() code, only set jump_count to 0 when reset jump count is true.
After all the jump code, in the same block of code, set reset_jump_count back to true.

I’m not sure if this would work, I’m kinda tired so I can’t really process exactly what I’m writing, but good luck nonetheless.
And I also don’t know why it fixed by turning max_jumps to 1 :stuck_out_tongue:

I tried that and it didn’t work, it still sets reset_jump_count to true before the player is off the ground. I’ll just set max_jumps to 1 but thanks for helping.

1 Like

If you aren’t too busy, could you show me the code you used?
The one based on what I said.
I might try to run it myself and debug it using prints.
But if this works and you like it then its fine…