Implementing a Coyote Timer into 2D platfomer (without using a State Machine) in Godot 4.7.2

Godot Version

4.7.2

Question

Hi everyone!

I am having trouble implementing a Coyote Timer which would allow a player to jump for a brief moment after walking off the edge of a platform. I am not currently using a finite state machine, for my project, as I am still new to using GDScript.

I appreciate all the help! Thank You!

extends CharacterBody2D
# allows for modification of AnimatedSprite2D in the Player Controller Script
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var jump_sound: AudioStreamPlayer2D = $JumpSound


const SPEED: float  = 200.0
const JUMP_VELOCITY: float = -350.0
const ACCELERATION = 600.0 
const FRICTION = 1000.0

#Every sec. our physics process will run 50 times, and delta will be how long it took between each frame (1/60th)
func _physics_process(delta: float) -> void:
	add_animation()
	add_gravity_and_variablejumping_and_normaljumping(delta)
	var input_axis := Input.get_axis("Left", "Right")
	# Get the input direction and handle the movement/deceleration.
	# Used custom input settings names 
	handle_acceleration(input_axis, delta)
	apply_friction(input_axis, delta)
	move_and_slide() #automatically applies delta, it multiplies velocity by delta, velocity is change in position over time
	handle_direction_that_player_faces(input_axis)




func add_animation():
		#Add Animation
	if velocity.x > 1 or velocity.x < -1:
		animated_sprite_2d.play("Walk") # or animated_sprite_2d.animation = "Walk"
	else: # if player is not moving then AnimatedSprite2D animation uses Idle animation
		animated_sprite_2d.play("Idle") # or animated_sprite_2d.animation = "Idle"

func add_gravity_and_variablejumping_and_normaljumping(delta):
		#Add the Gravity
	if not is_on_floor(): # this "is_on_floor():" function is a part of CharacterBody2D property
			velocity += get_gravity() * delta # gravity is also change in velocity over time
			animated_sprite_2d.play("Jump") #or animated_sprite_2d.animation = "Jump"
			#variable jumpinging mechannisim
			if Input.is_action_just_released("Jump") and velocity.y <JUMP_VELOCITY/2:
				velocity.y = JUMP_VELOCITY/2
	# Handle jump mechanic and sound when jumping.
	else: 
		if Input.is_action_just_pressed("Jump") and is_on_floor:
			velocity.y = JUMP_VELOCITY
			jump_sound.play()
			
func handle_acceleration(input_axis, delta):
	if input_axis !=0:
		velocity.x = move_toward(velocity.x, SPEED * input_axis, ACCELERATION * delta) # Acceleration is change in velocity over time, and it is not multiplied by delta

func apply_friction(input_axis, delta):
	if input_axis == 0:
		velocity.x = move_toward(velocity.x, 0, FRICTION * delta)  # Friction is another form of Acceleration , it is a change in velocity over time
		# or velocity.x = 0, for base script

func handle_direction_that_player_faces(input_axis):
			# Allows ot flip AnimatedSprite2D from left to right depennding on x = 1 or x = -1
	if input_axis == 1.0:
		animated_sprite_2d.flip_h = false
	#elif "otherwise"
	elif input_axis == -1.0:  
		animated_sprite_2d.flip_h = true

hi i fixed the problem. If the variables look odd, i just made a resource and named it PlayerMovementData for myself. So if you want to use this, just remove “PlayerMovementData” from the variables and place in the variables on the top as normal.


extends CharacterBody2D
# allows for modification of AnimatedSprite2D in the Player Controller Script
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var jump_sound: AudioStreamPlayer2D = $JumpSound
@export var movement_data: PlayerMovementData

#Every sec. our physics process will run 50 times, and delta will be how long it took between each frame (1/60th)
func _physics_process(delta: float) -> void:
	add_animation()
	add_gravity_and_variablejumping_and_normaljumping(delta)
	var input_axis := Input.get_axis("Left", "Right")
	# Get the input direction and handle the movement/deceleration.
	# Used custom input settings names 
	handle_acceleration(input_axis, delta)
	handle_air_acceleration(input_axis, delta)
	apply_friction(input_axis, delta)
	apply_air_resistance(input_axis, delta)
	move_and_slide() #automatically applies delta, it multiplies velocity by delta, velocity is change in position over time
	handle_direction_that_player_faces(input_axis)




func add_animation():
		#Add Animation
	if velocity.x > 1 or velocity.x < -1:
		animated_sprite_2d.play("Walk") # or animated_sprite_2d.animation = "Walk"
	else: # if player is not moving then AnimatedSprite2D animation uses Idle animation
		animated_sprite_2d.play("Idle") # or animated_sprite_2d.animation = "Idle"

func add_gravity_and_variablejumping_and_normaljumping(delta):
		#Add the Gravity
	if not is_on_floor(): # this "is_on_floor():" function is a part of CharacterBody2D property
			velocity += get_gravity() * movement_data.gravity_scale * delta # gravity is also change in velocity over time
			movement_data.coyote_timer -=delta
			animated_sprite_2d.play("Jump") #or animated_sprite_2d.animation = "Jump"
	else:
		movement_data.coyote_timer = movement_data.coyote_time
	#variable jumpinging mechannisim
	if Input.is_action_just_released("Jump") and velocity.y < movement_data.jump_velocity/2:
		velocity.y = movement_data.jump_velocity/2
	if Input.is_action_just_pressed("Jump") and movement_data.coyote_timer >0.0:
			velocity.y = movement_data.jump_velocity	
			movement_data.coyote_timer = 0.0
			jump_sound.play()
	
func handle_acceleration(input_axis, delta):
	if not is_on_floor(): return
	if input_axis !=0:
		velocity.x = move_toward(velocity.x, movement_data.speed * input_axis, movement_data.acceleration  * delta) # Acceleration is change in velocity over time, and it is not multiplied by delta
func handle_air_acceleration(input_axis, delta):
	if is_on_floor(): return
	if input_axis !=0:
		velocity.x = move_toward(velocity.x, movement_data.speed * input_axis, movement_data.air_resistance * delta)

func apply_friction(input_axis, delta):
	if input_axis == 0 and is_on_floor():
		velocity.x = move_toward(velocity.x, 0, movement_data.friction * delta)  # Friction is another form of Acceleration , it is a change in velocity over time
		# or velocity.x = 0, for base script
		
func apply_air_resistance(input_axis, delta):
	if input_axis ==0 and not is_on_floor():
		velocity.x = move_toward(velocity.x, 0, movement_data.air_resistance)
		
func handle_direction_that_player_faces(input_axis):
			# Allows ot flip AnimatedSprite2D from left to right depennding on x = 1 or x = -1
	if input_axis == 1.0:
		animated_sprite_2d.flip_h = false
	#elif "otherwise"
	elif input_axis == -1.0:  
		animated_sprite_2d.flip_h = true


\`\`\`