+1 vote

Hi, I am doing a 2d platformer tutorial by GDQuest. I am having a trouble modifying the code, so that my character would not change direction at all whatever is being input when jumping (like jumping in real world).
The base class is this one:

extends KinematicBody2D
class_name Actor

const FLOOR_NORMAL = Vector2.UP

export var speed := Vector2(400.0, 1000.0)
export var gravity := 3000.0

var velocity := Vector2.ZERO

And the Player class extends it:

extends Actor

func _physics_process(delta: float) -> void:
var is_jump_interrupted := Input.is_action_just_released("jump") and velocity.y < 0
var direction := get_direction()
velocity = calculate_move_velocity(velocity, speed, direction, is_jump_interrupted)
velocity = move_and_slide(velocity, FLOOR_NORMAL)

func get_direction() -> Vector2:
var x : float
var y = -1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0
var jump_right : bool
var jump_left : bool
var jump_in_place : bool
if Input.is_action_pressed("move_right") and is_on_floor():
    x = 1.0
    if Input.is_action_just_pressed("jump"):
        jump_right = true
elif Input.is_action_pressed("move_left") and is_on_floor():
    x = -1.0
    if Input.is_action_just_pressed("jump"):
        jump_left = true
var no_horizontal_direction_pressed_on_floor := not Input.is_action_pressed("move_right") and not Input.is_action_pressed("move_left") and is_on_floor() 
var both_horizontal_direction_pressed_on_floor := Input.is_action_pressed("move_right") and Input.is_action_pressed("move_left") and is_on_floor()
if no_horizontal_direction_pressed_on_floor or both_horizontal_direction_pressed_on_floor:
    x = 0.0
    if Input.is_action_just_pressed("jump"):
        jump_in_place = true
if not is_on_floor():
    if jump_right:
        return Vector2(1.0, y)
    if jump_left:
        return Vector2(1.0, y)
    if jump_in_place:
        return Vector2(0.0, y)
if is_on_floor():
    jump_right = false
    jump_left = false
    jump_in_place = false

return Vector2(

func calculate_move_velocity(
linear_velocity: Vector2,
speed: Vector2,
direction: Vector2,
is_jump_interrupted: bool
) -> Vector2:
var new_velocity := linear_velocity
new_velocity.x = speed.x * direction.x
new_velocity.y += gravity * get_physics_process_delta_time()
if direction.y == -1.0:
    new_velocity.y = speed.y * direction.y
if is_jump_interrupted:
    new_velocity.y = 0.0
return new_velocity

my character only jumps in place, meaning that when jumping it does not move horizontally at all and I don't know why it is so... Hope any of You guys can help me with it

in Engine by (13 points)

did you ever find an answer to your problem . im stuck on the same problem

Please log in or register to answer this question.

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.