Godot Version
`Godot 4.3
Question
Hi everyone,
I have problem in managing jump action in a 2D platformer. The problem is suring the falling after the jump action…the player fallls very very slowy…
This is the script to manage the falling:
@export var SPEED = 250.0
@export var JUMP_VELOCITY = 175.0
@export var jump_mult = -7.0
@export var gravity = 72
@export var falling_mul = 30
@export var player_controller: PlayerController
@export var animation_player: AnimationPlayer
@export var sprite: Sprite2D
var speed_mult = 30.0
var gravity_sys = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction = 0
func _input(event):
if event.is_action_pressed("Jump") and is_on_floor():
print('-------<mega jumping')
velocity.y += JUMP_VELOCITY * jump_mult
print('jumping to '+str(velocity.y))
#func die():
# animation_player.play("die")
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity.y += gravity_sys*delta*4
if velocity.y > 200:
velocity.y =200
direction = Input.get_axis("move_left","move_right")
if direction:
velocity.x =direction*SPEED*speed_mult
else:
velocity.x = move_toward(velocity.x,0, SPEED*speed_mult)
## manage direction
if direction == 1:
sprite.flip_h=false
elif direction ==-1:
sprite.flip_h=true
move_and_slide()
Your jump speed is -1225 (-7 * 175) and if your game runs at 60 fps you subtract from that speed: 9.8 * 4 *1/60 = 0.65.
It seems logical that it is difficult for him to go down.
I think your jump speed should be lower or you should have higher gravity.
1225/0.65 = 1884 frames / 60 = 31.41 seconds to reach speed 0.
1 Like
mmm, after having lower JUMP_VELOCITY to 110 despite the falling looks better, but jump reach much lower peak… 
What I think you have to do, instead of reducing the jump speed, is increase the gravity.
You have to balance your values so that the jump goes where you want and the fall is as fast as you want.
There are times when you may even want to apply a lower gravity while your character goes up than the one applied when your character goes down.
There are many types of jumps.
I don’t play Nintendo games but I think Mario falls much faster than he rises.
1 Like
ok, I am going to balance the gravity value and check if the jump is right to me
now I recognize falling is very slow…but i wouldn’t like to change JUMP_VELOCITY value
(Google translator)
I’m not an expert. The following may not interest you much, too much text :). It has been useful for me for the next time I want to program a jump.
Like you, controlling the jump only with the Godot example code is very difficult. As I said, you can vary the accelerations but that won’t give you total control either.
To have total control of the jump I think you can do the following. I put a drawing.
In point 1 I make a drawing of how I want the jump to be, it will reach a height of 200 pixels and will last 3 seconds, at the end you go down faster. That is the function of the space traveled (y axis) as a function of time (x axis).
The easiest way to represent that function is to approximate it by straight lines. That is what I do in 2. The straight lines are calculated with the formula of the straight line that passes through two points (or they are requested from chatgpt).
The speed is the derivative of space between time. If we derive each straight line we get the red lines of 3. In blue I have put a line of a continuous function because I do not want the speed to make those jumps and the changes to be smooth.
With this I have gone to Godot and I have created in the player a curve that represents the speed. I have used it in a code like the one I show.
The result is not very good :), you can see it here:
https://youtu.be/CpvLHZWDGoA. The important thing I think is the idea of how it could be done.
Edit: The approximation with straight lines does not seem like a good idea, it is better to define better approximations based on the different intervals. This can be solved by painting the curve in Godot in a much smoother way, without so many straight lines.
surely interesting approach, maybe a lil bit over load to make it.
Anyway, at moment I basically implemented the gravity into the _physics_process function:
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor() and not is_jumping:
#print('gravuty Nit in floor: '+str(gravity_sys*delta))
print('not is on floot. delta is:'+str(delta))
velocity.y += (gravity_sys*falling_mult)
if velocity.y > 200:
velocity.y=200
else:
print('player is on the floor')
is_jumping = false
outside of the function I implemented the jump skil:
func _input(event):
if event.is_action_pressed("Jump") and is_on_floor():
print('-------<mega jumping')
velocity.y -= (gravity_sys*jump_mult)#(JUMP_VELOCITY * jump_mult) - gravity_sys
is_jumping = true
The obtained result doesnt satisfy me because:
he reach too much fast the top
when starting to fall down It is quite slow…