+1 vote

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"):
        jump()

    if Input.is_action_just_released("ui_select"):
        stop_jump()

    vel = move_and_slide(vel, FLOOR)

func jump():
    vel.y = -jump_force

func stop_jump():
    if vel.y < -100:
        vel.y = -100
Godot version 3.2.3 stable
in Engine by (13 points)

1 Answer

+1 vote

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").start()

getnode("Tween").interpolateproperty( 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.TRANSSINE,Tween.EASEOUT) # changes the way the the property is animated

to learn more you can see these:
https://docs.godotengine.org/en/stable/classes/class_tween.html
https://www.youtube.com/watch?v=ofDcC3aux8Q

by (79 points)
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 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 webmaster@godotengine.org with your username.