I’m searching a way to prevent my rigidbody slide down slopes (and bumping too). I have already increased friction but my rigidbody can’t go up after (it’s the player).
I have do some research and find nothing viable. Only “tricks”.
Here’s what it looks like. I have tried with a circlecollider too but the problem stays.
(There is only the gravity applied to the rigidbody at this time)
So you want it to move down the slope only when you are moving the cube? You could try reducing the mass of the cube & the gravity, the movement may need rebalancing. And those changes will affect any jumping you may have in the game
What you are trying to achieve is a mix between realistic and unrealistic physics behavior while using a non-kinematic rigidbody… so, yes, you are going to have to use tricks.
Here are a few that might or might not work for you depending on your other requirements:
Make the rigidbody kinematic while there is no input.
Limit the rigidbody velocity while there is no input.
Force the rigidbody velocity to zero on x while there is no input.
Force the rigidbody velocity to zero on x while there is no input and the terrain is a slope (raycasthit → angle higher than threshold).
Another trick would be to set a high static friction and a low dynamic friction. But there is currently only one parameter for friction in 2D physics so that’s not supported.
I recently solved this problem. The key was first to detect when the rigidbody was “grounded” (IE, in contact with a suitably non-steep surface) and then use the surface normal at the contact point as a means to alter the direction of gravity specifically for the rigidbody in question, but only while it remains in contact with the surface.
I posted my full code here on stack exchange, and I’m pretty happy with the outcome.
What worked for me is to just reduce gravity to a really small number (like 0.01) while grounded. This means that the player can still be pushed by external forces while not pressing any movement keys.