As written in the title, I'm trying to implement precise movement for my playable character and I've run into a problem where my character's jump is overshooting the desired value I've set in the jump_height_pixels
variable.
Every function here except ground_check
is called every physics_process
frame.
I've followed this tutorial for my math calculations:
Any advice would be appreciated.
Here's the code that manages the character jump behavior:
extends Node
@export_category("Dependency Injections")
@export var character_body : CharacterBody2D
@export var state_chart : StateChart
@export_category("Jump Settings")
@export var jump_height_pixels : float
@export var jump_time_to_peak_seconds : float
@export_category("Fall Settings")
@export var jump_time_to_descent_seconds : float
var jump_time_to_peak_counter_seconds : float = 0.0
@onready var jump_velocity : float = ((2.0 * jump_height_pixels) / jump_time_to_peak_seconds) * -1.0
@onready var jump_gravity : float = ((-2.0 * jump_height_pixels) / (jump_time_to_peak_seconds * jump_time_to_peak_seconds)) * -1.0
@onready var fall_gravity : float = ((-2.0 * jump_height_pixels) / (jump_time_to_descent_seconds * jump_time_to_descent_seconds)) * -1.0
func ground_check(_delta):
if !character_body.is_on_floor():
state_chart.send_event("on_air")
return
state_chart.send_event("on_ground")
func get_gravity() -> float:
return jump_gravity if character_body.velocity.y < 0.0 else fall_gravity
func apply_gravity(delta):
character_body.velocity.y += get_gravity() * delta
func jump_character(delta):
var is_jump_button_held = Input.get_action_strength("jump")
character_body.velocity.y = jump_velocity
jump_time_to_peak_counter_seconds += delta
if jump_time_to_peak_counter_seconds >= jump_time_to_peak_seconds:
state_chart.send_event("jump_completed")
jump_time_to_peak_counter_seconds = 0.0
if is_jump_button_held == 0: #equal to false
state_chart.send_event("jump_completed")
jump_time_to_peak_counter_seconds = 0.0