Setting up smooth movement on slopes

Someone wanted me to help them with a 2D platformer she was working on. She got the player to move around using the movement scripts in the Unity 2D project, but she couldn’t figure out how to fix some odd movement on slopes. The player tends to slide down when standing still on the slope; the player also tends to move very slowly up slopes.

The system she had in mind allows the player to move in a constant speed up and down inclines, similar to in Super Mario World. I looked online for something similar, but I wasn’t able to implement what she wanted. Does anyone have any solutions which allows for a similar movement scheme to what I described above?

P.S. The scripts she’s using are in C#, so if anyone has any pseudocode it would be preferable if it was in that language.

1 Like

I’m bumping this because i have asked a similar question on these forums, unity answers and stackexchange without having any answers, it’s as if nobody really know how to handle slopes other than some “hacky” way such as freezing the character when there are no inputs, which doesn’t look right at all, i’m surprised, this is such a standard thing for all 2d games to have, no unity tutorials or sample scenes are even mentioning it.

I was in the process of porting my UE4 game to Unity thinking it would be a much better solution for 2D but here i am, stuck on how to handle slopes for a whole week, something that i never even had to think about with UE4’s default character controller, i’ve searched the whole internet and i’m starting to loose my mind. There isn’t even anything on the asset store!

There’s a couple ways to tackle this, but I’ll just outlet the easiest solution. I’m going to assume you’re not using a rigidbody as your character controller, since rigidbodies as character controllers are evil.

If you’re applying a downwards gravity force every frame, your character is being translated downwards where it will clip the slope. This collision will be detected, and to resolve it the physics engine will translate your character out of the slope, moving him down it slightly. Over multiple frames this causes him to “slide” down the surface.


Above the controller in red is pulled down by gravity (to the blue dotted one) and translated out to the green one.

When you attempt to move up the slope, what is actually happening is the reverse of the above. Your character starts as the green dotted one, and moves into the slope, then is translated upwards. On steep slows this will make your movement much slower.

So how do we solve this? Instead of just moving your character left and right, move them parallel to the floor. That way, they’ll just slide in the direction of whatever slope they’re standing on. To solve the gravity problem you can disable gravity when you know your character is already standing on the ground, instead of constantly applying gravity every frame. I expand on this idea in a post I wrote awhile back.

By and large if you want super smooth controlling characters like Mario and whatnot it’s important to learn how the underlying logic works for basic functions.

1 Like

Thank you for the detailed answer Iron Warrior.

Unfortunately i am using a Rigidbody as physics interactions are a key element in my game and i’m trying to recreate the exact feel that i had in UE4, so far the character’s controls feels just like they did with UE4 except for the damn slopes which was all taken care of in UE4’s character controller, but i can’t give up on physics.

I have seen a wonderful 2D controller tutorial by Sebastian Lague which tackles slopes but like you, he isn’t using any physics.

There has to be a simple way to deal with slopes using a Rigidbody… I’m surprised the Rigidbody2D doesn’t handle this by default!

Are you just wanting the controller to exert a force on other rigidbodies? If you just want him to exert a weight on objects he’s standing on/touching you could use the CC’s OnControllerColliderHit to detect collisions and use the contact point to exert a force from that position. From the gif you’ve posted (which I assume is your game in UE4) it looks like the character should just be pushing objects around with his feet/body.

As an alternative, have you looked at PhysicsMaterial2D at all? You might be able to solve the problem with friction…but I don’t use rigidbody much so I’m not sure.

I have looked at physical materials and friction was useless, a high enough value to prevent the sliding will make it too difficult to move, it would be impossible to go up a slope.

I have read a bit about OnControllerColliderHit and thought it might be a solution, i wanted to try that and use a character controller component but then realized it wouldn’t work with 2d coliders, i could try it if i made my own 2d controller.

The only reason i want physics in my game is to be able to interact with rigidbodies such as a floating raft or a car with suspension, i want it to look as if the character has weight and i want to push stuff around, didn’t think that would be possible without a rigidbody.

Bumping this, as I have the same problem and I’ve been stuck on this for a week with no easy solution when using rigidbody2d.

What I did was use a trigger on the character. if I’m touching the ground, don’t apply force down, if I’m not that apply it. ALso I set the constraints to nothing for freeze position and set rotation to all. It runs really smoothly.