How to stop KinematicBody character from sliding on slopes?

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

Hi, I’m trying to throw together a first person character controller with a KinematicBody and I’m having trouble keeping it from sliding down slopes.

At the moment, I am using a setup like self.velocity = self.move_and_slide(velocity), and as expected the character slowly slides down any slope its put on when gravity. However, the sliding persists even when I have the stop_on_slope parameter set, and the character is on a slope that’s shallow enough to be considered a floor.

Correct me if I’m wrong, but I assume this is due to the fact that the function is still returning the collision’s resultant velocity, which would push the character sideways a small bit due to gravity pushing the character into the slope.

How would I go about preventing move_and_slide from pushing the character sideways when gravity is effecting them on a shallow slope? Would it be easier to use move_and_slide_with_snap instead?

:bust_in_silhouette: Reply From: scrubswithnosleeves

move_and_slide_with_snap ( Vector2 linear_velocity, Vector2 snap, Vector2 up_direction=Vector2( 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )

move_and_slide_with_snap Moves the body while keeping it attached to slopes. Similar to move_and_slide.

As long as the snap vector is in contact with the ground, the body will remain attached to the surface. This means you must disable snap in order to jump, for example. You can do this by setting snap to (0, 0) or by using move_and_slide instead.