Physic Prevent rigidbody sliding

Hi,

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)

Thanks for your help.

2208046--146727--slope.gif

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.

5 Likes

The above are good options, there’s also:

  • Set gravity-scale to zero when you want the movement to stop.
  • Turn-on X/Y axis constraints when you want the movement to stop.
  • Set the gravity-scale and/or linear-drag to be proportional to the angle of the slope (collision-normal) i.e. steeper = < gravity-scale.

As has been said; these are not tricks, they’re rules for the physics in your world. :slight_smile:

7 Likes

Thanks all for your answers.

I will try your options.

@MevMay Thanks for tip, turning rigidbody constrains on and off works like a charm.

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.

4 Likes

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.

1 Like