Strange Kinematic Body Behavior on slopes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gamepro5

I recorded the issue because its hard to describe. The video can be found here.

I am desperate for help, I have been struggling with this for weeks.

The code in the video can be examined further here (the relevant player code is here).

Thank you to anyone who takes the time to help me.
(This uses the most recent version of Godot: 3.2.2)

Have you attempted to use move_and_slide_with_snap() at all?

RedBlueCarrots | 2020-07-15 11:32

Of course. You can see that in the code. That is what I am currently using, not using move_and_slide_with_snap makes it not slide down a bit but instead it makes the player jump after climbing a slope.

Gamepro5 | 2020-07-15 18:48

For those still struggling with this issue. Make Gravity the last thing you calculate, I struggles with this for a long time but fixed it by simply moving the gravity calculation to the bottom. Make sure to use the is_on_floor() signal also.

if not is_on_floor():
    velocity.y -= gravity * delta

move_and_slide_with_snap worked the best for me also.

LordBoots | 2022-12-02 12:58

Holy crap thank you for this! Weird slope behavior has been giving me fits for literally months and I had no idea how to track down what was causing all the odd stuff. Calculating gravity after everything else seems to have fixed all of it.

karltha | 2023-01-14 23:49

:bust_in_silhouette: Reply From: btuso

Just in case somebody else runs into this problem. The issue is that while you can set move_and_slide to not slide down slopes, the gravity is still used to calculate the movement against the slope’s normal.

A simple fix for me was to cancel out the sideways movement caused by the gravity pushing the character against the slope. This can be calculated with the floor normal.

A simple example:

velocity = .... 
velocity = player.move_and_slide(velocity, Vector3.UP, true)

The previous snippet would result in downwards movement when moving sideways and different speeds for going up and down the slope, like this weird movement

However, if we take the floor normal and multiply it by the affecting gravity, we can cancel out the effect

var correction = Vector3(0,0,0)
if player.is_on_floor():
    correction = Vector3(0,1,0) - player.get_floor_normal()
    correction *= GRAVITY
    correction *= -1
velocity = ....
velocity = player.move_and_slide(velocity + correction, Vector3.UP, true)

Our player will now behave as if it was walking on a flat surface.