The problem occurs when the jump key is released before starting the jump, therefore the jump cannot be cancelled and will go to the max height.
You can fix this by tracking how many frames there are between the jump key being pressed and the jump key being released. When the jump starts, increment a counter each frame until the counter equals the amount of frames the jump key was held for, then cancel the jump velocity.
Here is an example (Doesn't involve coyote time however):
extends KinematicBody2D
var frame_time: int = 0
export var floor_direction = Vector2(0, -1)
export var max_speed: int = 1500
export var acceleration: int = 50
export var jump_height: int = -2150
export var gravity: int = 80
var jump_buffered = false
var jump_start_frame : int
var jump_length : int
var jump_release_counter: int = 0
var is_jumping : bool = false
var motion = Vector2()
func _physics_process(delta):
frame_time += 1
motion.y += gravity
var friction = false
# Left and Right movement
if Input.is_action_pressed("ui_right"):
motion.x += max_speed
$MainSprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
motion.x -= max_speed
$MainSprite.flip_h = true
else:
friction = true
#Jumping and buffers
if Input.is_action_just_pressed("ui_up"):
jump_start_frame = frame_time
startBuffer()
if Input.is_action_just_released("ui_up"):
jump_length = frame_time - jump_start_frame
if is_jumping:
jump_length -= 1
if jump_length == 0:
is_jumping = false
jump_length = 0
if motion.y < 0: #Check needs to be made so jump cannot be cancelled when falling
motion.y *= 0.5 #Set the Jump Short
if is_on_floor():
if friction == true:
motion.x = lerp(motion.x, 0, 0.2)
if jump_buffered:
is_jumping = true
motion.y = jump_height
else:
if friction == true:
motion.x = lerp(motion.x, 0, 0.1)
#############################################
motion.x = clamp(motion.x , -max_speed, max_speed)
motion = move_and_slide(motion, floor_direction)
func startBuffer():
jump_buffered = true
yield(get_tree().create_timer(0.1), "timeout")
jump_buffered = false