#When sliding on a slope it does not maintain inertia and I am going crazy

12 messages · Page 1 of 1 (latest)

rancid rampart
#

I recently started working with Godot 4 and I've been wanting to replicate the Jump King game to learn how to program, but I'm having trouble getting it to slide when it falls on a slope and instead of falling suddenly, it maintains its speed.

#

Now i made a new project to develop this mechanics, i adjunt a video with the scene.

knotty sparrow
digital grotto
#

I can’t really remember rn but I think if the player has 0 velocity on an angled surface you need to set the velocity to like -1 or 1 so that the player can start accelerating. You might also need to change what angle is considered a floor in the character body node settings.

Alternatively using the floor angle you know when the player is touching a slope so you can code your own thing where the player starts falling and accelerating in the x direction. This might be better because then it’s probably easier to copy jump king

knotty sparrow
#

the main problem here is the fact that sliding doesn't actually change the direction the velocity property has.

#

i might suggest to use a rigid body instead.

#

actually, sliding doesn't seem to affect the velocity at all.

#

the only way the velocity seems to be automatically changed is when motion mode is "grounded" and either landing on the ground or bumping on the ceiling.

digital grotto
#

ok i got it working and i dont think you need to change any of the character body settings

#
extends CharacterBody2D


var speed = 300
var jump_power = -700
var gravity = 1100
var gravity_limit = 1100
var drag = 4000
var stunned = false
var slide_speed = 2
var sliding_speed_limit = 900


func _physics_process(delta):
    # Regular Movement Code. Not based from jump king
    velocity.y = min(velocity.y + gravity * delta, gravity_limit)
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = jump_power
    var direction = Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * speed
    # If this is applied at all times then you will instantly stop
    # In Jump King I think this is applied when on floor
    elif round(get_floor_angle() * 180 / PI) != 45 and not stunned:
        apply_friction()
    
    move_and_slide()
    
    # Check if floor is slope
    if round(get_floor_angle() * 180 / PI) == 45:
        # Stun player. Use this to prevent movement and stuff
        stunned = true
        # Change Player velocity to follow slope
        velocity = get_real_velocity()
        # Sliding Speed Modifier
        var acceleration = (delta * slide_speed) * max(velocity.length(), 1)
        # Get Direction of player velocity
        var dir = 1
        if velocity.x < 0:
            dir = -1
        # Increase Velocity depending on acceleration and limit it
        if velocity.length() < sliding_speed_limit:
            velocity = Vector2(velocity.x + dir * acceleration, velocity.y + acceleration)
        # If velocity is 0 Set vel to 1 or -1
        # depending on which way slope is facing
        if get_floor_normal().x > 0.1 and round(velocity.x) == 0:
            velocity.x = 1
        elif get_floor_normal().x < -0.1 and round(velocity.x) == 0:
            velocity.x = -1
    # When we land unstun the player
    elif is_on_floor():
        if stunned:
            # Play SPLAT animation
            pass
        stunned = false


func apply_friction():
    velocity.x = 0