Skip to content

Instantly share code, notes, and snippets.

@sjvnnings
Last active June 28, 2025 09:49
Show Gist options
  • Save sjvnnings/5f02d2f2fc417f3804e967daa73cccfd to your computer and use it in GitHub Desktop.
Save sjvnnings/5f02d2f2fc417f3804e967daa73cccfd to your computer and use it in GitHub Desktop.
An easy to work with jump in Godot
extends KinematicBody2D
export var move_speed = 200.0
var velocity := Vector2.ZERO
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()
velocity = move_and_slide(velocity, Vector2.UP)
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
@Zuhyy
Copy link

Zuhyy commented Nov 2, 2024

How would I implement this jump script into my player script with state machine in godot v4.3?

@makeryangcom
Copy link

I found an issue in version 4.4. Everything looks normal if no actual jump animation is associated. However, when the animation is added, there is always a delay, causing the player to stay in the air for a while before the animation appears.

class_name Player extends CharacterBody3D

@onready var player_collision: CollisionShape3D = $Collision
@onready var player_body: Node3D = $Body
@onready var player_skin: Node3D = $Body/Skin
@onready var player_camera: PlayerCamera = $Camera
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var animation_tree: AnimationTree = $AnimationTree
@onready var move_state_machine = $AnimationTree.get("parameters/MoveStateMachine/playback")

@export var player_data: Dictionary
@export var body_height: float = 1.7
@export var walk_speed: float = 4.0
@export var run_speed: float = 6.0
@export var jump_height: float = 1.6 
@export var jump_time_to_peak: float = 0.4 
@export var jump_time_to_descent: float = 0.3 
@export var jump_velocity : float = ((body_height * jump_height) / jump_time_to_peak) * -1.0
@export var jump_gravity : float = ((-(body_height) * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0
@export var fall_gravity : float = ((-(body_height) * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0

func _ready() -> void:
	print("[player]:_ready")

func _process(_delta: float) -> void:
	pass

func _physics_process(_delta: float) -> void:
	move_logic(_delta)
	jump_logic(_delta)
	move_and_slide()

func set_move_state(state_name: String) -> void:
	move_state_machine.travel(state_name)

func move_logic(_delta: float) -> void:
	var input_direction := Input.get_vector("left", "right", "forward", "backward").rotated(-player_camera.camera_main.global_rotation.y)
	var player_direction = Vector3(input_direction.x, 0, input_direction.y)
	var is_running: bool = Input.is_action_pressed("shift")
	var speed = run_speed if is_running else walk_speed
	if input_direction != Vector2.ZERO:
		velocity.x = player_direction.x * speed
		velocity.z = player_direction.z * speed
		var target_angle = -input_direction.angle() + PI/2
		player_skin.rotation.y = rotate_toward(player_skin.rotation.y, target_angle, 6.0 * _delta)
		set_move_state("default_men_run_loop" if is_running else "default_men_walk_loop")
	else:
		velocity.x = move_toward(velocity.x, 0, speed)
		velocity.z = move_toward(velocity.z, 0, speed)
		set_move_state("default_men_stand_idle_loop")

func jump_logic(_delta: float) -> void:
	if is_on_floor():
		if Input.is_action_just_pressed("jump"):
			velocity.y = -jump_velocity
	else:
		set_move_state("default_men_stand_jump_start")
	var gravity = jump_gravity if velocity.y > 0.0 else fall_gravity
	velocity.y -= gravity * _delta

@LiamTheMagician
Copy link

There something I just can't figure out, my implementation looks fine, but jump height seems to be disregarded completely, in Godot 4.3:

extends CharacterBody2D

@export var speed = 300.0

@export var jump_height : float = 0.5
@export var jump_time_to_peak : float = 1.0
@export var jump_time_to_descent : float = 0.5

@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_peak)) * -1.0

func get_input():
	return Input.get_axis("player_left", "player_right")

func get_gravity_2D():
	return jump_gravity if velocity.y < 0.0 else fall_gravity

func jump():
	velocity.y = jump_velocity

func _physics_process(delta):
	velocity.y += get_gravity_2D() * delta
	velocity.x = get_input() * speed
	
	if Input.is_action_just_pressed("player_jump") and is_on_floor():
		jump()
	
	move_and_slide()
	

@n33do
Copy link

n33do commented Jun 27, 2025

My jump behaves inconsistently some times for example when I printed the values of y velocity every frame. Normally it looks like this 0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
-134.16667175293
-128.333343505859
-122.500007629395
-116.66667175293
-110.833335876465
-105.0
-99.1666641235352
-93.3333282470703
-87.4999923706055
-81.6666564941406
-75.8333206176758
-69.9999847412109
-64.1666488647461
-58.3333168029785
-52.4999847412109
-46.6666526794434
-40.8333206176758
-34.9999885559082
-29.166654586792
-23.3333206176758
-17.4999866485596
-11.6666536331177
-5.83332014083862
0.00001319249441
0.00001319249441
0.00001319249441
0.00001319249441
0.00001319249441
0.00001319249441
0.00001319249441
0.0
but sometimes it looks like this .0
0.0
0.0
0.0
0.0
0.0
0.0
-134.16667175293
-128.333343505859
-111.666679382324
-95.0000152587891
-78.3333511352539
-61.6666831970215
-45.0000152587891
-28.3333492279053
-11.6666822433472
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
4.99998426437378
0.0
0.0
And that other velocity curve doesn't go as high does anyone have any Idea what I should do? turning up the physics tick rate seemed to lessen the issue but not eliminate it.

@Intrivus
Copy link

My jump behaves inconsistently some times for example when I printed the values of y velocity every frame. Normally it looks like this 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -134.16667175293 -128.333343505859 -122.500007629395 -116.66667175293 -110.833335876465 -105.0 -99.1666641235352 -93.3333282470703 -87.4999923706055 -81.6666564941406 -75.8333206176758 -69.9999847412109 -64.1666488647461 -58.3333168029785 -52.4999847412109 -46.6666526794434 -40.8333206176758 -34.9999885559082 -29.166654586792 -23.3333206176758 -17.4999866485596 -11.6666536331177 -5.83332014083862 0.00001319249441 0.00001319249441 0.00001319249441 0.00001319249441 0.00001319249441 0.00001319249441 0.00001319249441 0.0 but sometimes it looks like this .0 0.0 0.0 0.0 0.0 0.0 0.0 -134.16667175293 -128.333343505859 -111.666679382324 -95.0000152587891 -78.3333511352539 -61.6666831970215 -45.0000152587891 -28.3333492279053 -11.6666822433472 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 4.99998426437378 0.0 0.0 And that other velocity curve doesn't go as high does anyone have any Idea what I should do? turning up the physics tick rate seemed to lessen the issue but not eliminate it.

I have made a video on this problem: https://youtu.be/YBNSPAvmLoA?si=QVyVY_x29919cFVN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment