0 votes

Hey all!

So in my platformer code, my character jumps just fine. However, when he just walks off an edge, he falls a lot faster than when he is jumping. I am implementing my jumping by having a jumpForce variable, a gravity variable, and then constantly adding the gravity to the player's movement:

//apply the gravity
    movement.y += gravity;

    //check for input
    if (Input.IsActionPressed("moveRight"))
    {
        movement.x = moveSpeed;
        isFacingRight = true;
    }
    else if (Input.IsActionPressed("moveLeft"))
    {
        movement.x = -moveSpeed;
        isFacingRight = false;
    }
    else
    {
        movement.x = 0;
    }

    if (IsOnFloor())
    {
        if (Input.IsActionJustPressed("moveJump"))
        {
            //set the jump force
            movement.y = -jumpForce;
        }
    }

    MoveAndSlide(movement, upDirection);

Is there a better way to fo about this so that the player doesn't just fall quickly off of the platforms?

Thank you!

in Engine by (21 points)

1 Answer

0 votes
Best answer

You're not using the return-value of move_and_slide so movement grows bigger and bigger in y-direction each frame due to gravity. As long as the platform blocks the way, that has no consequences, but once you move off of it, you'll fall with full momentum! It's easy to fix though: `movement = MoveAndSlide(movement, upDirection);

by (10,628 points)
selected by
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.