Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Physic Prevent rigidbody sliding

Discussion in '2D' started by Pob_Fr, Jul 16, 2015.

  1. Pob_Fr

    Pob_Fr

    Joined:
    Jun 12, 2015
    Posts:
    6
    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.
     

    Attached Files:

  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,569
    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
     
  3. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    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.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,513
    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. :)
     
    rzeeryp2 and BowtieBuck like this.
  5. Pob_Fr

    Pob_Fr

    Joined:
    Jun 12, 2015
    Posts:
    6
    Thanks all for your answers.

    I will try your options.
     
  6. scarpelius

    scarpelius

    Joined:
    Aug 19, 2007
    Posts:
    954
    @MevMay Thanks for tip, turning rigidbody constrains on and off works like a charm.
     
  7. Bropoc

    Bropoc

    Joined:
    Sep 30, 2014
    Posts:
    8
    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.
     
    sammarin, CaelumCB and tytbone like this.
unityunity