0 votes

When I press 'W' button, my player was flies endlessly.
Code:

extends Node

var can_jump = false;

var Settings = {
    'left'    :    'ui_left',
    'right'   :    'ui_right',
    'top'     :    'ui_up',
    'down'    :    'ui_down',
    'speed'   :    200,
    'jump'    :    200,
    'jump_time':   0,
    'max_jump_time':  0.1  #in seconds

    }

var velocity = Vector2();
var grav = get_gravity_scale();
var grounded = false;

func _physics_process(delta):
    if Input.is_action_pressed(Settings.left):
        velocity.x = -Settings.speed;
    elif Input.is_action_pressed(Settings.right):
        velocity.x = Settings.speed;
    else:
        velocity.x = 0;
    if Input.is_action_just_pressed(Settings.top) and can_jump and Settings.jump_time < Settings.max_jump_time:
        Settings.jump_time += delta;
        velocity.y -= Settings.jump;
    elif Input.is_action_just_released(Settings.top):
        can_jump = false;
    if grounded:
        can_jump = true;
        Settings.jump_time = 0; 
    else:
        velocity.y += grav;
    set_linear_velocity(velocity);

func _on_Area2D_area_entered(area):
    grounded=true;
    pass
in Engine by (18 points)

1 Answer

0 votes

I think the problem is here:

if Input.is_action_just_pressed(Settings.top) and can_jump and Settings.jump_time < Settings.max_jump_time:
    Settings.jump_time += delta;
    velocity.y -= Settings.jump;
elif Input.is_action_just_released(Settings.top):
    can_jump = false;

You used Input.is_action_just_pressed() therefore Settings.jump_time += delta; is only called once and the time limit for jumping is never reached.

I think you just have to replace Input.is_action_just_pressed() with Input.is_action_pressed() and it should do the trick ;)

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