I'm currently making a game and I made this simple script that checks when a player holds down the up arrow key so that the longer you hold the button, the further upwards you jump. It works fine and all, until you decide to jump again mid-air, then the gravity of the is_action_just_pressed is added into the player's total gravity which is kind of a problem.
I'm kind of new to GDScript, so a little help would really be appreciated!
Here's my (bad) code:
var gravity : int = 1800
var vel : Vector2 = Vector2()
func _physics_process(delta):
vel.x = 0
vel = move_and_slide(vel, Vector2.UP)
vel.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
vel.y -= jumpforce
gravity -= 500
if Input.is_action_just_released("jump"):
gravity += 500
There isn't anything in godot for checking if the player is holding down a button and I'm not sure what is wrong with my code, so I have no idea what is causing this issue or how to fix it.
There isn't anything in godot for checking if the player is holding down a button
Instead of using is_action_just_pressed
and is_action_just_released
which tell you if the action was pressed or released since the last frame, use is_action_pressed
which tell you if the action is currently pressed.
It works fine and all, until you decide to jump again mid-air, then the gravity of the is_action_just_pressed is added into the player's total gravity which is kind of a problem.
The issue is that you are not keeping track if player jumped. For example:
if Input.is_action_just_pressed("jump") and is_on_floor():
jumped = true
vel.y -= jumpforce
gravity -= 500
if Input.is_action_just_released("jump") and jumped:
jumped = false
gravity += 500
With jumped
declared at the top of the script, similar to gravity
and vel
.