This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+2 votes


I'm trying to make my character jumps configurable, where you can define jump height and duration. I've checked numerous tutorials about that but the calculations shown there don't seem to be accurate.

For example i configured my jump height to be 200px but after checking it ingame it appears to be a bit lower (~188px).
I am not sure if the math formula is inaccurate or maybe i am not taking some of the settings into consideration?

Tracked height debug values:
enter image description here

My example code:

extends KinematicBody2D

# Configuration Variables
var jump_height = 200
var jump_duration = 0.3

# Helper Variables
var current_velocity = Vector2()
var gravity
var jump_velocity

# Debug Variables
var highestposition = 0
var initialposition

func _ready():
    gravity = 2 * jump_height / pow(jump_duration, 2)
    jump_velocity = -sqrt(2 * gravity * jump_height)

    # DEBUG - Save original jump position
    initialposition = get_position().y

func _physics_process(delta):
    current_velocity.y += gravity * delta
    current_velocity = move_and_slide(current_velocity, Vector2(0, -1))

    # DEBUG - Track the peak position
    var currentpos = initialposition - get_position().y
    if currentpos > highestposition:
        highestposition = currentpos

func _input(event):
    if event.is_action_pressed("jump"):
        current_velocity.y = jump_velocity

        # DEBUG - Reset peak position
        highestposition = 0

Any help would be appreciated

in Engine by (14 points)

I did a small modification - swapped lines in physicsprocess()

current_velocity = move_and_slide(current_velocity, Vector2(0, -1))
current_velocity.y += gravity * delta

And now it overshoots it by reaching 210px jump height.
Console output showing that jump reaches circa 210px of jump height

And i noticed that when you make jump duration longer, it becomes more accurate.

For example for jump that peaks after 1 second:
enter image description here

And for 2 seconds:
enter image description here

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.