Issues with character momentum while creating a 2D platformer

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AshTheKindra

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
:bust_in_silhouette: Reply From: Inces

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.

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

AshTheKindra | 2021-10-28 18:10