0 votes

So you know how in your typical 2D platformers like Super Meat Boy or Mario when you're running to either the left or right and then you jump, your momentum carries over to your jump, and when you let go of the directional buttons, you still move to the right or left because of that momentum? So I'm making a 2D platformer and I'm trying to refine the controls. I have most of the controls work the way I want to. The jump even works right, when I run and jump while holding down the left or right button, my character goes to the left or right. However, if I run and jump and let go of any of the direction keys, my character stops in mid air and drops like a brick because momentum didn't carry over to my jump. I have tried to put acceleration and friction into the jumping values, but that doesn't seem to work The coed for jumping is below, any help would be appreciated.

func _physics_process(delta: float) -> void:
_get_input()
velocity.y += gravity * delta
velocity = move_and_slide(velocity, Vector2.UP)
if Input.is_action_just_pressed("jump"):
    if is_on_floor() or nextToWall():
        velocity.y = jump_speed
    if not is_on_floor() and nextToRightWall():
        velocity.x -= wallJump + jumpWall
    if not is_on_floor() and nextToLeftWall():
        velocity.x += wallJump + jumpWall
if nextToWall():
    $AnimatedSprite.play("Wall Slide")
if nextToWall() and velocity.y > 30:
    velocity.y = 30
    $AnimatedSprite.play("Wall Slide")
if not is_on_floor() and not nextToWall():
    if velocity.y < 0:
        $AnimatedSprite.play("Jump")
    if velocity.y > 0:
        $AnimatedSprite.play("Fall")
if Input.is_action_just_released("jump"):
    if sign(velocity.y) != 1:
        velocity.y = 0
Godot version 3.3
in Engine by (39 points)

1 Answer

0 votes
Best answer

You need to reflect on what You are really trying to implement - momentum that is decreasing while in the air or prohibition of diagonal movement control while in the air. I didn't play supermeatboy, but I recollect Mario falling down very fast after letting go of direction pad and direction pad having weaker influence while Mario was in the air. Is this behavior You need ?

If so You want to add more conditionals to input event : "when input is left/right and when is not on floor nor on wall velocity is increased by half amount of normal increment" and "when no directional input is pressed and is not on floor velocity remains velocity, but when is on floor velocity becomes zero"

besides, If You aim for nice anf fluent character control, than You still should implement some acceleration and clamp it.

by (7,305 points)
selected by

Yes I did mean to cut the velocity in half when you let go of a directional button, thank you for your help.

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.