This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

(in Godot 4.0.beta2)

So I've got some basic movement made, but I'm noticing a problem when my player (CharacterBody2D) walks off a sloped ledge. It looks like a hop, but I'm pretty sure it's just that the vertical movement isn't included in the velocity vector.

How do I keep my player's (CharacterBody2D) vertical movement when going off sloped ledges?

enter image description here

-Code when on ground-

func ground(delta: float) -> void:
    #--inputs--#
    input_direction.x = Input.get_axis("Terra_Left","Terra_Right"); #left and right arrows
    input_direction.y = -int(Input.is_action_just_pressed("Terra_Jump")); #space bar

    #--physics and code stuff--#
    velocity.x = round(speed) * input_direction.x;
    velocity.y = jumpPower * input_direction.y;

    move_and_slide();

    #--sprite info--#
    sprTerra.rotation = lerp(sprTerra.rotation,Vector2.UP.angle_to(get_floor_normal()),0.3);


    #changing state
    if (!is_on_floor()):
        changeState(state.air); #you are now in the air and not on the ground

-Code when in air-

func air(delta: float) -> void:
    #--inputs--#    
    input_direction.y = -int(Input.is_action_just_pressed("Terra_Jump"));
    var jumpHold: bool = Input.is_action_pressed("Terra_Jump");
    var jumpReleased: bool = Input.is_action_just_released("Terra_Jump");

    #--physics and code stuff--#
    add_gravity(delta);
    #if let go of jump, add extra gravity to pull down player   
    if jumpsLeft && -input_direction.y>0:
        velocity.y = jumpPower * input_direction.y;

    if jumpReleased:
        jumpsLeft = max(0,jumpsLeft-1);
    if (!jumpHold && velocity.y<0): #if not holding jump button and moving up   
        velocity+= (GLOBAL.grav*2*delta);

    move_and_slide();

#--sprite info--#
#rotation
    sprTerra.rotation = lerp(sprTerra.rotation,0.0,0.07);


#changing state
    if (is_on_floor()):
        jumpsLeft=MaxjumpCount;
        changeState(state.ground);
Godot version 4.0.beta2
in Engine by (23 points)

1 Answer

0 votes

Never mind, I got it. Ended up adding the velocity.y when going up and down slopes, and had to check for collisions a little oddly but it's workin.

by (23 points)
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.