Characterbody2D movement not smooth when going downwards on a slope

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

Hi all, can I please get some help with coding gravity?!

My Characterbody2D moves down a slope as if he went right and then down (gravity), instead of a smooth sliding motion. This is my code:

 func _physics_process(delta):
	if is_on_floor():
		speed.y = 0
	else:
		speed.y += 40
	my_state.process_state(delta)
	set_velocity(speed)
	move_and_slide()

I experimented by putting set_floor_snap_length(0) and/or apply_floor_snap() just before move_and_slide() with no luck

The process_state basically just calls the function below with dir depending on what direction the Character2D is going

func move (dir : int):
    match dir:
        direction.right:
            speed.x = lerp(speed.x, max_speed, acceleration)    
        direction.left:
            speed.x = lerp(speed.x, -max_speed, acceleration)
        direction.up:
            speed.y -= jump_speed

If it matters, I’m initialising my variables to the following values

func initialise_variables():
	acceleration = 0.35
	max_speed = 600
	jump_speed = 1300
	speed = Vector2(0,0)

Thanks for your help!

Perhaps have a look at the tutorial example provided by Godot: Kinematic character (2D) — Godot Engine (stable) documentation in English

putper | 2023-06-06 00:04

what if you try speed.y += 40*delta instead of speed.y += 40, this way the acceleration is based on the delta and not fixed to 40 pixels per frame?

godot_dev_ | 2023-06-06 16:08