• Godot Help
  • I need help with smoothening an aspect of my 2D (sidescroller) movement code

(Godot 3.5.2, semi-new to Godot, made 1 small game so far)

Hey there, im a developer in Godot who is making a small game for the Bigmode Game Jam, it’s going decent so far for me, but im having extreme trouble with smoothening the movement of my Long Jump. It feels like no matter how hard I try, I can’t make it feel janky.

The issue right now is that whenever the player longjumps (not referring to regular jump, that works fine), instead of smoothly moving from point A to point B, the player just teleports to point B instantly, and it looks really bad.

I’m at the point where I might just scrap the long-jump movement, but I figured I may aswell post here for help as a last resort beforehand to see if I can find a solution to get the code to make the longjump smooth.
If any of you know how to fix this, please let me know.
Thanks.

Here’s the code:
extends KinematicBody2D

const UP_DIRECTION := Vector2.UP
onready var sprite = $AnimatedSprite

export var speed := 100
export var gravity := 400
export var jump_strength := 150
export var max_fall_speed := 180
var _velocity := Vector2.ZERO
var wallJump := 190
var bounceoff := 30
export var double_jump_strength = 140
var maxJumps := 2
var jumpsMade := 0

var acceleration = 200
var longjumpdistance = 500

var WJlimit := 1
var WJcount := 0

func _physics_process(delta: float) -> void:
var _horizontal_direction = (
Input.get_action_strength("move_right")
- Input.get_action_strength("move_left")
)

if Input.is_action_pressed("move_left"):
	sprite.set_flip_h(true)
if Input.is_action_pressed("move_right"):
	sprite.set_flip_h(false)



_velocity.x = _horizontal_direction * speed
_velocity.y += gravity * delta





var is_falling := _velocity.y > 0.0 and not is_on_floor()
var is_double_jumping := Input.is_action_just_pressed("jump") and is_falling
var is_jumping := Input.is_action_just_pressed("jump") and is_on_floor()
var is_sliding = is_on_wall() and not is_on_floor() and _horizontal_direction
var is_jump_cancelled := Input.is_action_just_released("jump") and _velocity.y < 0.0
var is_idling := is_on_floor() and is_zero_approx(_velocity.x)
var is_running := is_on_floor() and not is_zero_approx(_velocity.x)
var is_bursting := is_on_wall() and WJcount == 0 and not is_on_floor() and Input.is_action_pressed("move_right") or Input.is_action_pressed("move_left") and Input.is_action_just_pressed("jump")
var is_longjumping := is_on_floor() and not is_on_wall() and Input.is_action_just_pressed("longjump")

if is_jumping:
	jumpsMade += 1
	_velocity.y = -jump_strength
elif is_double_jumping:
	jumpsMade += 1
	if jumpsMade <= maxJumps:
		_velocity.y = -double_jump_strength
elif is_jump_cancelled:
	_velocity.y = 0.0

if is_longjumping:
	_velocity.x = lerp(_velocity.x, _horizontal_direction * longjumpdistance, acceleration * delta)

if is_sliding:
	max_fall_speed = 40
else:
	max_fall_speed = 180
	
	
if is_on_wall() and WJcount == 0 and not is_on_floor() and Input.is_action_pressed("move_left") and Input.is_action_just_pressed("jump"):
	_velocity.y = -wallJump
	WJcount = 1
	
if is_on_wall() and WJcount == 0 and not is_on_floor() and Input.is_action_pressed("move_right") and Input.is_action_just_pressed("jump"):
	_velocity.y = -wallJump
	WJcount = 1



if _velocity.y > max_fall_speed:
	_velocity.y = max_fall_speed

_velocity = move_and_slide(_velocity, UP_DIRECTION)

if is_jumping:
	sprite.play("Jump")
elif is_sliding:
	sprite.play("Wallslide")
elif is_running:
	sprite.play("Run")
	jumpsMade = 0
	WJcount = 0
elif is_idling:
	jumpsMade = 0
	WJcount = 0
	sprite.play("Idle") :

It looks like your var is_longjumping is only true for one cycle, when the button is just pressed, so it's only changing the velocity one time.

    fire7side alright, how would I go about fixing it though? do I just redo the code to be something like “if is_longjumping and Input.is_action_just_pressed(“jump”):” then instead or something?

      fire7side I don’t know exactly what you mean by “turn it false”, like I don’t know how I’d implement that into the code.

      There’s not really anything inside the if statement I can turn to false.

        fire7side I can’t seem to figure it out, and still don’t know what you’re getting at by the t variable, I guess I just don’t understand the lerp() function all-to-well right now.

        I tried doing something along the lines of “velocity.x = lerp(velocity.x, delta * acceleration)” and then “position = position.linear_interpolate(_velocity, delta * acceleration)” with the acceleration value turned down to 5, but no matter what I do it still just teleports the character

        In the same “if is_long jumping and Input.is_action_just_pressed(“longjump”):” code, I can easily add a “_velocity.x = -Longjumpheight” line in the indentation and that’ll push the character up smoothly, but doing pretty much the same on the X axis makes it teleport, and I don’t see why that is.

        This is the formula in the doc on the link. It's using a vector2.
        sprite_position = start_position.lerp(end_position,t)

        So you get the postions in the if clause

        if button just pressed and blah blah:
            is_long_jumping = true
            lj_start = this.position
            lj_end = lj_start * horizontal_direction * lj_distance
            t = 0
        if is_long_jumping:
          
           this.position = lj_start.lerp(lj_end, t)
           t += long_jump_timestep
           if t>1:
               is_long_jumping = false

        You'll have to put those variables at the top with the other ones that are named.