0 votes

I am making a 2d platformer. The player can jump higher by holding the jump button. The play stops moving up when the jump button is released, but the player can slow their jump by mashing the jump button. How can I stop this? This is the code.

extends KinematicBody2D

var motion = Vector2()
var airjump = 0.2
var airtime = 0.2
var cutjumpheight = 0
const MOE = 0.2
const CTIME = 0.2
const SPEED = 300
const GRAVITY = 60
const JUMPHEIGHT = -750
const UP = Vector2(0,-1)

#move right and left whenthe arrow keys are presed
func _physics_process(delta):
    motion.y += GRAVITY
    if Input.is_action_pressed("ui_right"):
        motion.x = SPEED
    elif Input.is_action_pressed("ui_left"):
        motion.x = -SPEED
    else:
        motion.x = 0

    #Timer to dimtermine if the player inputed the jump button recently
    airjump -= get_process_delta_time()
    if Input.is_action_just_pressed("ui_up"):
        airjump = CTIME
    #Timer to see if the player was on the floor recently
    airtime -= get_process_delta_time()
    if is_on_floor():
        airtime = MOE

    #Tells the game to jump
    if airtime > 0 && airjump > 0:
        motion.y = JUMPHEIGHT
    #makes the player stop moving up when jump is released
    if Input.is_action_just_released("ui_up") && motion.y < 0:
        motion.y = 0


    motion = move_and_slide(motion, UP)
    pass
in Engine by (27 points)

1 Answer

0 votes

Basically just ignore new keypresses if the player is not on the floor

by (368 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 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.