Forked from sjvnnings/better_jumping_character_example.gd
Last active
March 8, 2025 15:28
-
-
Save Deledrius/027d61f1d0574a7db0a93592d1384704 to your computer and use it in GitHub Desktop.
An easy to work with jump in Godot 4.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
@export var move_speed = 200.0 | |
@export var jump_height : float | |
@export var jump_time_to_peak : float | |
@export var jump_time_to_descent : float | |
@onready var jump_velocity : float = ((2.0 * jump_height) / jump_time_to_peak) * -1.0 | |
@onready var jump_gravity : float = ((-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0 | |
@onready var fall_gravity : float = ((-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0 | |
func _physics_process(delta): | |
velocity.y += get_gravity() * delta | |
velocity.x = get_input_velocity() * move_speed | |
if Input.is_action_just_pressed("jump") and is_on_floor(): | |
jump() | |
move_and_slide() | |
func get_gravity() -> float: | |
return jump_gravity if velocity.y < 0.0 else fall_gravity | |
func jump(): | |
velocity.y = jump_velocity | |
func get_input_velocity() -> float: | |
var horizontal := 0.0 | |
if Input.is_action_pressed("left"): | |
horizontal -= 1.0 | |
if Input.is_action_pressed("right"): | |
horizontal += 1.0 | |
return horizontal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment