How to slide on slope with RigidBody2D node

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

For some reason, I’m using a RigidBody2D node to manipulate the character.
Horizontal movement is fine, however, when encountering a slope, I don’t know how to make the character slide along the slope.
In character2d, move_and_slide method can solve it, but in my case, I find it very difficult.

:bust_in_silhouette: Reply From: Christian Baune

I started from the template.

Still not solved how to have the character be launched by a counter slope.
So, it can slide, but not be “launched”

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")

var sliding = false
var can_snap = true
func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		if sliding:
			velocity.y += gravity/2 * delta
		else:
			velocity.y += gravity * delta

	if Input.is_action_pressed("ui_down") and is_on_floor() and abs(get_floor_normal().y)!=1:
		var slide_dir
		if get_floor_normal().x>0:
			slide_dir=get_floor_normal().rotated(TAU/4)
		else:
			slide_dir=get_floor_normal().rotated(-TAU/4)
		sliding = true
		velocity += slide_dir*delta*SPEED*5

	# Handle Jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		sliding=false

	var direction := Input.get_axis("ui_left", "ui_right")
	if direction:
		sliding=false
	if not sliding:
		if direction:
			velocity.x = direction * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED*delta)
		velocity.x=clamp(velocity.x,-SPEED*1.5,SPEED*1.5)
	else:
		velocity.x=clamp(velocity.x,-SPEED*3,SPEED*3)
		velocity.x = move_toward(velocity.x, 0, SPEED/10*delta)
	if sliding and is_on_floor() and abs(get_floor_normal().y)!=1 and sign(get_floor_normal().x)!=sign(velocity.x):
		print("stop snapping")
		can_snap = false
		get_tree().create_timer(3).timeout.connect(func ():
			print("allowing snap")
			can_snap=true
			)
		floor_snap_length=0
	else:
		if can_snap:
			print("re-snap")
			floor_snap_length=16
	move_and_slide()