added jump buffer

This commit is contained in:
zymsbgt 2024-08-17 22:49:59 +08:00
parent 96c5bde897
commit 01f24d5f6b

View file

@ -5,6 +5,7 @@ extends CharacterBody2D
var jump_count = 1
@export var jump_strength = 100
var is_touching_floor : bool = true
var jump_buffer_timer : float
var coyote_timer : float = 0.2 # 200 millisecond buffer
var input_direction = 0 #To keep track of which direction we where moving in last frame
@ -19,7 +20,7 @@ func get_input(delta):
input_direction = -1
elif right:
input_direction = 1
else :
else:
input_direction = 0
# This line updates the player's velocity
@ -30,24 +31,32 @@ func get_input(delta):
is_touching_floor = true
jump_count = 1
coyote_timer = 0.2
if jump_buffer_timer > 0:
Jump()
if not is_on_floor():
if jump_buffer_timer > 0:
jump_buffer_timer -= delta
if coyote_timer > 0.0:
coyote_timer -= delta
else:
jump_count = 0
if Input.is_action_just_pressed("player_jump") and jump_count > 0:
velocity.y = -1 * jump_strength
jump_count = 0
is_touching_floor = false
if Input.is_action_just_pressed("player_jump"):
if jump_count > 0:
Jump()
else:
jump_buffer_timer = 0.2 # set a timer to jump again once the player has reached the ground, provided the jump is is still being held down
elif Input.is_action_pressed("player_jump"): #Fall less fast if we keep holding the button
velocity.y += -1 * 25
func _physics_process(delta):
get_input(delta)
velocity.y += gravity
move_and_slide()
print(jump_count)
# print(jump_count) # Uncomment for debugging
func Jump():
velocity.y = -1 * jump_strength
jump_count = 0
is_touching_floor = false