Any (relatively) simple way to handle 2d slopes with rigidbody2d?

This is something I couldn’t find anywhere on the web, I know there are plenty of ways to climb 2d slopes using custom physics systems relying on raycasts, but what is the right way to avoid your character stopping/sliding down slopes while using Unity physics from rigidbody2d?

Yes sorry I forgot to update, actually I found a solution good enough for me, on this video:

On the first comment a user posted a snippet of code:
// @NOTE Must be called from FixedUpdate() to work properly
void NormalizeSlope () {
	// Attempt vertical normalization
	if (grounded) {
		RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 1f, whatIsGround);
		
		if (hit.collider != null && Mathf.Abs(hit.normal.x) > 0.1f) {
			Rigidbody2D body = GetComponent<Rigidbody2D>();
			// Apply the opposite force against the slope force 
			// You will need to provide your own slopeFriction to stabalize movement
			body.velocity = new Vector2(body.velocity.x - (hit.normal.x * slopeFriction), body.velocity.y);

			//Move Player up or down to compensate for the slope below them
			Vector3 pos = transform.position;
			pos.y += -hit.normal.x * Mathf.Abs(body.velocity.x) * Time.deltaTime * (body.velocity.x - hit.normal.x > 0 ? 1 : -1);
			transform.position = pos;
		}
	}
}

I use only this line to prevent the sliding:

body.velocity = new Vector2(body.velocity.x - (hit.normal.x * slopeFriction), body.velocity.y);

and it simply works, I used slopes up to 45 degrees with a slopeFriction of 0.5. The rest of the code is not necessary and actually it didn’t work with how I set up the rest of the code, especially since I define movement through rigidbody.velocity, and that transform.position at the end would be out of place. The only thing left to complete this would be to adjust the x and y movement speed when moving on the slopes according to the Sin and Cos fractions of the normal angle, to maintain a constant movement speed and not slow down a bit when climbing the slope.

EDIT: I want to update this thread, after some days, I don’t know what went wrong but that code does no longer work for me, I even tried to impose the rigidbody velocity to zero but for some reason it keeps sliding down, probably I changed something with the colliders while doing other stuff.
I found another solution however, I would say it is quite bad, but it works even better than the previous one for me. Basically when I detect a slope and no input, I freeze the rigidbody x movement:

if (downCollision.angle != 0 && isGrounded && horizontal == 0)
            {
                rb.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
            }
            else if (downCollision.angle != 0 && isGrounded)
            {
                rb.velocity = new Vector2(Mathf.Cos(downCollision.angle * Mathf.Deg2Rad) * horizontal , Mathf.Sin(downCollision.angle * Mathf.Deg2Rad)*horizontal);
            }

And at the start of the controller script, I reset the freeze in case there is some input:

if(horizontal != 0)
            rb.constraints = RigidbodyConstraints2D.FreezeRotation;

As I said, it is quite bad in my opinion as solution, but again, it is quick and works if you are not too choosy :wink: