How do i stop my character from jumping higher than it should when i spam the jump key?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mintteacup

When I spam the jump key my character sometimes (I might just not be consistent in hitting the jump button) jumps higher than usual and I have no idea why.
Here is a video showing this: Video

extends KinematicBody2D

var velocity = Vector2(0,0)
var bufferedJump = false
var coyoteJump = false

onready var jumpBufferTimer = $jumpBufferTimer 
onready var coyoteTimer = $coyoteTimer

const SPEED = 500 
const GRAVITY = 45 
const JUMPFORCE = -1000 

func _physics_process(delta):
	if Input.is_action_pressed("right"):
		velocity.x = SPEED
		$Sprite.play("walk")
		$Sprite.flip_h = false
	elif Input.is_action_pressed("left"):
		velocity.x = -SPEED
		$Sprite.play("walk")
		$Sprite.flip_h = true
	else: 
		$Sprite.play("idle")
	
	if not is_on_floor():
		$Sprite.play("air")
	
	velocity.y += GRAVITY
	
	if is_on_floor() or !coyoteTimer.is_stopped():
		if Input.is_action_just_pressed("jump") or bufferedJump:
			velocity.y = JUMPFORCE
			bufferedJump = false
	else:
		if Input.is_action_just_pressed("jump"):
			bufferedJump = true
			jumpBufferTimer.start()
	
	var was_on_floor = is_on_floor()
	
	velocity = move_and_slide(velocity, Vector2.UP)
	
	if was_on_floor and not is_on_floor():
		coyoteTimer.start()
	
	velocity.x = lerp(velocity.x, 0, 0.2)


func _on_jumpBufferTimer_timeout():
	bufferedJump = false
:bust_in_silhouette: Reply From: Badger

I think it is because the coyoteTimer is activated based on whether the player was on the floor. So it also activates when the player jumps. So you might need to check whether the player Jumped last Frame.

If was_on_floor and not is_on_floor() and not jumpedLastFrame:

I also recommend you make your code simpler, this can be done by saving conditions to a variable,
example:

entered_air = was_on_floor and not is_on_floor()
entered_air_without_jump = entered_air and no_jump_last_frame
If entered_air_without_jump:
    coyoteTimer.start()

So it could look something like:

var was_on_floor = true
var no_jump_last_frame = true


func jumpState():
    var entered_air = was_on_floor and not is_on_floor()
    entered_air_without_jump = entered_air and no_jump_last_frame

    If entered_air_without_jump:
        coyoteTimer.start()

    var coyote_timer_running = !coyoteTimer.is_stopped()
    no_jump_last_frame = true

    if Input.is_action_just_pressed("jump") or bufferedJump:

        if coyote_timer_running:
            velocity.y = JUMPFORCE
            bufferedJump = false
            no_jump_last_frame = false

        elif not bufferedJump:
            bufferedJump = true
            jumpBufferTimer.start()


    was_on_floor = is_on_floor()