Skip to content

Instantly share code, notes, and snippets.

@sjvnnings
Last active May 2, 2024 22:32
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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
@Kronkmeister
Copy link

Kronkmeister commented Jul 12, 2021

This is great, thank you. But how do I normalize this, so diagonal movement isn't faster?

And while we are at it: I am also struggling to find out how I can make the movement direction go based on camera direction, more like an fps. (I turned this into working for a 3D environment)

@sjvnnings
Copy link
Author

This is great, thank you. But how do I normalize this, so diagonal movement isn't faster?

And while we are at it: I am also struggling to find out how I can make the movement direction go based on camera direction, more like an fps. (I turned this into working for a 3D environment)

Thanks for asking, here's a quick mockup of what this looks like in a 3D controller:

extends KinematicBody

const MOUSE_SENSITIVITY := 0.05

export var move_speed = 5.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
onready var jump_gravity : float = (-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)
onready var fall_gravity : float = (-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)

onready var cam := $path/to/camera

var velocity : Vector3

func _ready():
	OS.window_fullscreen = true
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _process(delta):
	if Input.is_action_just_pressed("ui_cancel"):
		get_tree().quit()
	
	velocity.y += get_gravity() * delta
	
	var horizontal := 0.0
	var vertical := 0.0
	
	if Input.is_action_pressed("left"):
		horizontal -= 1.0
	if Input.is_action_pressed("right"):
		horizontal += 1.0
	if Input.is_action_pressed("up"):
		vertical -= 1.0
	if Input.is_action_pressed("down"):
		vertical += 1.0
	
	var input = Vector3(horizontal, 0.0, vertical).normalized()
	
	if Input.is_action_just_pressed("jump"):
		jump()
	
	input = $cam.global_transform.basis.xform(input)
	input.y = 0

	var hvel = input.normalized() * move_speed
	
	velocity.x = hvel.x
	velocity.z = hvel.z
	
	velocity = move_and_slide(velocity, Vector3.UP)

func jump():
	velocity.y = jump_velocity

func get_gravity() -> float:
	return jump_gravity if velocity.y > 0.0 else fall_gravity

You'll have to figure out camera rotation yourself, but this should be a good starting point. Basically, you construct an input vector from your directional inputs, then you use your camera's basis to transform a local vector (our input) into world space. So, if the character is moving forward, you'd have something like Vector3(0,0,1), and when you transform that with the camera, it would return it's forward vector in world space (which would be equal to camera.global_transform.basis.z).

Of course, you don't want the player to move upwards just because they're looking up! That's what the jump is for. So, we set input.y = 0 after the transform, basically flattening it to just the left/right (x) and forward/backward (z) axes. Then, just normalize that, and you have your input both normalized, and relative to the camera!

Hope that helped, if you haven't already, I would highly recommend the official FPS tutorial on the Godot Docs. They cover a lot of useful stuff like this in there. Just replace the jump code there (JUMP_SPEED and GRAVITY) with the jump code in the example above! It'll make it a lot easier to work with.

@Kronkmeister
Copy link

Thank you, worked very well!

@twe4ked
Copy link

twe4ked commented Aug 16, 2021

Just linking the related YouTube video so I can find it from this Gist too.

Thanks for this 🙏

@mariogodot
Copy link

dosent work

@Ramontique
Copy link

In Godot 4.0.1 I have set jump_height to 66 but I can't jump on top of tiles that are 64 pixels high. I have to set jump_height to 67 to make the jump. Is this behavior to be expected or is something wrong with my code?

@sjvnnings
Copy link
Author

I haven't tested this code with Godot 4.0 yet, but I can't see why it would be different, unless CharacterBody3D has substantially changed. Could you send a screenshot of your set up (i.e. the tiles you want to jump, the character, and the collision shapes you're using)?

@Ramontique
Copy link

Just made a minimalist project using only your code converted to Godot 4.0.2 and it seems to work without any issue.

@Ramontique
Copy link

Ramontique commented Apr 10, 2023

Hm I did notice something weird after all: jump_height 64, 63, 62 all jump over 64 pixels. Might be something wrong with Godot 4 physics after all. /edit: same behavior in 3.5.

@sjvnnings
Copy link
Author

Hmmm, that's strange, I haven't noticed any problems in Godot 3.x yet, and this code should be mathematically pure as it's derived from real-world physics equations. Could you send a screenshot of your project or a minimal reproduction project?

@Ramontique
Copy link

https://www.dropbox.com/s/x2fk6yg589q10gw/Jump%20height%20test%203.5.zip?dl=0

Here's a link to a minimal reproduction project in Godot 3.5. As you can see I have jump_height set to 62 and the player can jump over 64 height of blocks.

@default1200
Copy link

I tried this but it doesn't work for some reason. I get Division by zero in operator '/' error
also an error at function: _ready reffering to the onready var

@Frodo-tech
Copy link

I tried this but it doesn't work for some reason. I get Division by zero in operator '/' error also an error at function: _ready referring to the onready var

Are you sure you initialized all the related variables? if either time_to_descend or time_to_peak are 0 when the node is ready (whence why the error occurs at the _ready() function) you'll get a division by zero error.

@Jazztache
Copy link

Would there be a way to get this working in Godot 4.X?

@KyleRConway
Copy link

KyleRConway commented Aug 16, 2023

Would there be a way to get this working in Godot 4.X?

This works on my end. It's the base movement template from Godot 4.0 with these concepts built in.


extends CharacterBody2D

@onready var screen_size = get_viewport_rect().size

const SPEED = 600.0
#const JUMP_VELOCITY = -600.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

# Get the gravity from the project settings to be synced with RigidBody nodes.
#var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
#var gravity = 980

func _physics_process(delta):
	print(jump_gravity,fall_gravity)
	# Add the gravity.
	if not is_on_floor():
		velocity.y += get_gravity() * delta

	# Handle Jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = jump_velocity
		print("JUMP")

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
		slow_friction(delta)
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	print(direction)
	
	# Screenwrap	
	#position.x = wrapf(position.x, 0, screen_size.x)
	#position.y = wrapf(position.y, 0, screen_size.y)

	move_and_slide()

func slow_friction(d):
	velocity.x -= .02 * d
	print(velocity)
	print("SLOW")

func get_gravity() -> float:
	return jump_gravity if velocity.y < 0.0 else fall_gravity

@kwhitejr
Copy link

kwhitejr commented Oct 20, 2023

@Jazztache

Would there be a way to get this working in Godot 4.X?

Here is a minimalist implementation for Godot 4.x.x. I also included air jumps because reasons.

extends CharacterBody2D

@export var move_speed : float = 200.0
@export var air_jumps_total : int = 1
var air_jumps_current : int = air_jumps_total

@export var jump_height : float = 30
@export var jump_time_to_peak : float = 0.5
@export var jump_time_to_descent : float = 0.25

@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_horizontal_velocity() * move_speed
	
	if Input.is_action_just_pressed("jump"):
		if is_on_floor():
			jump()
		if air_jumps_current > 0 and not is_on_floor():
			air_jump()

	var direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * move_speed
	else:
		velocity.x = move_toward(velocity.x, 0, move_speed)

	move_and_slide()

func get_gravity() -> float:
	return jump_gravity if velocity.y < 0.0 else fall_gravity

func jump():
	air_jumps_current = air_jumps_total
	velocity.y = jump_velocity
	
func air_jump():
	air_jumps_current -= 1
	velocity.y = jump_velocity

func get_horizontal_velocity() -> float:
	var horizontal := 0.0
	
	if Input.is_action_pressed("move_left"):
		horizontal -= 1.0
	if Input.is_action_pressed("move_right"):
		horizontal += 1.0
	
	return horizontal

@Perndoe
Copy link

Perndoe commented Feb 17, 2024

I found out that physics frame rates affects the code, which makes the script now really inconsistent. When I set the jump to 256 (twice the height of the character's body) it jump roughly 9 pixels higher above the platform that is 3 times my body, looking somewhat like this:
image
(this is with 60fps)

And if I set the game settings to 120 fps, it jumps normally (probably would affect higher jumps)

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