How to prevent double jump with coyote jump

Godot Version

ver4.3

Question

`Im trying to implement coyote jump and it works except player can jump again at peak of jump. How can I prevent this?

extends PlayerState

@export var _animation_player: NodePath
@onready var animation_player:AnimationPlayer = get_node(_animation_player)

# Called when the node enters the scene tree for the first time.
func enter(_msg := {}) -> void:
	animation_player.play("player_fall")
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func physics_update(delta: float) -> void:
	player.flip_sprite()

	if not is_zero_approx(player.get_input_direction()): #If Player is moving, calculate Player's velocity.x
		player.velocity.x = lerpf(player.velocity.x, player.get_input_direction() * player.speed, player.acceleration * delta)
	else: #If Player is not moving, Player's velocity.x = 0
		player.velocity.x = lerpf(player.velocity.x, 0, player.air_friction * delta)

	player.velocity.y += player.set_gravity() * delta	#gravity * delta #Gravity Calculation
	player.move_and_slide()

	if player.coyote_jump_counter >= 0.0:
		player.coyote_jump_counter -= delta
		player.coyote_jump = true
	if player.coyote_jump_counter <= 0.0:
		player.coyote_jump_counter = 0.0
		player.coyote_jump = false

	if Input.is_action_just_pressed("jump") and player.coyote_jump == true:
		state_machine.transition_to("Jump")
		player.coyote_jump = false

	if Input.is_action_just_pressed("jump") and player.jump_buffer == false:	#If player jump midair and Jump Buffer is false, set Jump Buffer true
		player.jump_buffer = true
		await get_tree().create_timer(player.jump_buffer_time).timeout
		player.jump_buffer = false

	if player.is_on_floor():
		player.coyote_jump = true
		player.coyote_jump_counter = player.coyote_jump_time

		if is_zero_approx(player.get_input_direction()):	#If Player is not moving, IDLE STATE
			state_machine.transition_to("Idle")
		else:	#If Player is moving, MOVE STATE
			state_machine.transition_to("Run")

		if player.jump_buffer == true:		#If Jump Buffer is true, JUMP STATE
			state_machine.transition_to("Jump")
			player.jump_buffer = false
	pass

`

On jump input set a variable called jumped to true. If you try to coyote jump check if you already jumped via the jumped variable. If you are on the ground set the jumped variable to false.

var jumped := false

func jump():
   if jumped or remaining_coyote_jump_time <= 0:
      return

   velocity.y = jump_velocity
   jumped = true

func _physics_process(delta):
   if is_on_floor():
      jumped = false
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.