How do I make a good looking "hold to jump higher" jump

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By larybig27

Hello. I am looking to make a jump kind of like the game Hollow Knight where jump height is determined by how long you hold the jump key down. I’ve looked at a few resources and also tested stuff by myself. I have something working but it’s not looking the way I want it to. Something about the way the player falls down after doing a short jump is just weird. It looks great if you do a full jump though. I am fairly new to the engine so if you have a solution, just explain it in baby terms. Thanks!

Here are the variables

var vel = Vector2()
var FLOOR = Vector2.UP
var jump_force = 600
var gravity = 80

func _physics_process(delta):
    vel.y += gravity
    if Input.is_action_just_pressed("ui_select"):

    if Input.is_action_just_released("ui_select"):

    vel = move_and_slide(vel, FLOOR)

func jump():
    vel.y = -jump_force

func stop_jump():
    if vel.y < -100:
        vel.y = -100
:bust_in_silhouette: Reply From: PeterA

Your code is well thought out for what you are trying to do but the result will look choppy because you are directly changing the value of the speed. Usually you want to avoid that by changin the accelaration, for smoother movement.

If for some reason you can’t do that then you must change the speed gradually and not just set it to an arbirtary value because it will look very unnatural.

In godot the tool we use to change any property gradually over a certain period in time is the node Tween.

Step 1) add a child node Tween
Step 2) change the stop_jump function to something like this

func stop_jump():
    if vel.y < -100:
       get_node("Tween").interpolate_property(self,'vel:y',vel.y, -100, 0.35,Tween.TRANS_SINE,Tween.EASE_OUT)

get_node(“Tween”).interpolate_property( self, #where the property you want to manipulate is
‘vel:y’, #the name of the property
vel.y, #the initial value
-100, #the value you will end on
0.35, # how much time it will take
Tween.TRANS_SINE,Tween.EASE_OUT) # changes the way the the property is animated

to learn more you can see these: