added coyote timer

This commit is contained in:
zymsbgt 2024-08-17 22:26:53 +08:00
parent e04b50f15b
commit 543394bd19
3 changed files with 28 additions and 16 deletions

View file

@ -1,20 +1,20 @@
extends CharacterBody2D
@export var speed = 340;
@export var gravity = 50;
@export var jump_count = 2;
@export var speed = 340
@export var gravity = 50
var jump_count = 1
@export var jump_strength = 100
var jump_count_current = 2;
var is_touching_floor : bool = true
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
func get_input():
func get_input(delta):
var left = Input.is_action_pressed("player_left")
var right = Input.is_action_pressed("player_right")
if left and right:
input_direction = input_direction
input_direction = 0
elif left:
input_direction = -1
elif right:
@ -22,20 +22,32 @@ func get_input():
else :
input_direction = 0
# This line updates the player's velocity
velocity.x = input_direction * speed
if is_on_floor(): # rest the jump count
jump_count_current = jump_count
if is_on_floor():
# reset the jump count
is_touching_floor = true
jump_count = 1
coyote_timer = 0.2
if Input.is_action_just_pressed("player_jump") and jump_count_current > 0:
if not is_on_floor():
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_current = jump_count_current-1
elif Input.is_action_pressed("player_jump"): #Fall less fast if we keep holding the button
jump_count = 0
is_touching_floor = false
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()
get_input(delta)
velocity.y += gravity
move_and_slide()
print(jump_count)