Search Unity

Rigidbody keeps sliding

Discussion in 'Editor & General Support' started by snicholls, Oct 28, 2009.

Thread Status:
Not open for further replies.
  1. snicholls

    snicholls

    Joined:
    Mar 19, 2009
    Posts:
    229
    Hi all, I have a standard cube with a rigid body attached and I cannot seem to stop it moving when its resting. It always slides slightly so that if I leave the game alone for a while it will move all the way over to the other side of the screen.

    I have tried playing with the friction values etc with no luck. The platform the cube is on is completely flat.
     
    AdmiralThrawn likes this.
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Is there any script on the object that adds forces, etc?
     
  3. towcar

    towcar

    Joined:
    May 2, 2009
    Posts:
    106
    I have the same problem

    If you download the FPS Rigid Body example it comes with a demo of the Rigid bodys useage, and in the demo it automatically slides and its kinda old. Help
     
  4. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    Try this.

    In the rigid body.
    Play around with its mass (try increasing it).
    Try increasing its linear dampening (which is called drag I think).

    Give it a physics materiel.

    For the physics material
    Sets its static and dynamic material to 1.0
    Also set the friction combine parameter to use maximum.
     
  5. pimpin

    pimpin

    Joined:
    Nov 11, 2012
    Posts:
    6
    to stay still
    rigidbody.kinematic = true;
    to move again
    rigidbody.kinematic = false;
     
    Evildrake303, lazzyman and PREMmike like this.
  6. Toasttify

    Toasttify

    Joined:
    Mar 27, 2012
    Posts:
    143
    Don't do that unless you feel like constantly having to awaken rigidbodies from their kinematic state.

    Also this thread is 4 years old so he probably found a solution already...
     
    Solringen likes this.
  7. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    Maybe, but I have the exact same problem as the OP. In fact I have temporarily abandoned the game idea because of it. No mater what I did to the moving object and the resting surfaces the moving object would either wobble off or slide off. I tried huge mass, materials I even tried the turning off kinematic which worked best BUT the logic to try and decide when to do that made it temperamental.

    So I too would like to know how best to achieve this. I have a great game just sat in a directory waiting for the answer.
     
  8. meat5000

    meat5000

    Joined:
    Mar 5, 2013
    Posts:
    118
    I'm having the same trouble. This thread was started in 2009. It's 2013 now and still no answer to this widespread problem? A lot of projects have been abandoned for this rigidbody sliding and bouncing defect.
     
  9. acorrow

    acorrow

    Joined:
    Jan 4, 2015
    Posts:
    32
    Yes I know this is a bit of a necro, but hey, someone resurrected this from 4 years in the grave so.....

    I ran into this seemingly out of no where, and did a bit of searching then had a "DOH!" moment. Make sure that your rigid body isn't a Child of your camera, and the camera set to follow the ridigbody. This is what happened to me. I was organizing all my Levels "standard" objects into one game object, and accidentally made the character a child of the camera I setup to follow it. This will constantly "slide" your character based on the last movement speed.

    Hope this helps someone
     
    TheTrueAlpha and BrandyStarbrite like this.
  10. TheButtaSauce

    TheButtaSauce

    Joined:
    Apr 12, 2014
    Posts:
    8
    Figured this out.

    Change the following settings in your projects physics settings.

    Edit > Project Settings > Physics

    Solver Iteration Count: at least 10 (I use 15)
    Enable Adaptive Force: On (I turn this on, although it isn't necessary)

    Hope this helps! ^_^
     
  11. GeminiGamer

    GeminiGamer

    Joined:
    May 23, 2015
    Posts:
    1
    Hey, this works...!!!! Thanks a lot. Was stuck on this for hours. :D
     
    monxter likes this.
  12. Datapax

    Datapax

    Joined:
    Sep 10, 2015
    Posts:
    4
    Hi all, I know this is pretty old but, for anyone like me that may end up here, there is another thing you can try that I didn't find in the posts above.

    So, if you are still here and nothing worked, check if you have <AnimationController> component attached to your object. If you have - disable it and try to run the game. If the issue disappears you should check and change the settings of your animations controller and/or animations!

    It worked for me.
     
    pepperopigeon likes this.
  13. bigoMay

    bigoMay

    Joined:
    Sep 14, 2013
    Posts:
    6
    Hello all,

    I had a similar problem with RigidBody objects. In my case, the problem was that the surface where the objects were resting had a MeshCollider, which for some reason was producing a slight sliding of the objects. Changing the collider of the surface to a simple BoxCollider fixed the problem.

    I am not sure about the reason for this, but my guess is that the MeshCollider was overloading the physics engine with too many precise calculations, resulting in the sliding on the objects.
     
  14. Dier0

    Dier0

    Joined:
    Jan 2, 2017
    Posts:
    1
    Hi all,
    I made script for moving platforms, not by changing position, but moving it. This fixed sliding problem and moving is sinusoidal.

    It worked for me.
     

    Attached Files:

  15. day_of_money

    day_of_money

    Joined:
    Feb 26, 2015
    Posts:
    1
    I know this is super late however i ran into this problem as well and could not find the answer because none of the above worked for me so i keep searching and find an idea on how to fix this problem. and with that in mind this is what i came up with to fix it.
    Hope it helps.
     

    Attached Files:

    AdmiralThrawn likes this.
  16. GGLio

    GGLio

    Joined:
    Apr 30, 2017
    Posts:
    1
    just increase the linear drag and increase the speed
     
  17. MurFiesta

    MurFiesta

    Joined:
    Jun 18, 2017
    Posts:
    1
    I ran into this issue building the space shooter tutorial. I increased the drag to 10 and the speed to 200 and that solved my issue - thanks GGlio/ others who provided the same advice.
     
  18. Ojiba

    Ojiba

    Joined:
    Aug 18, 2017
    Posts:
    1
    I think i have finally solved this. I was having the same issue and racking my brain about it. Turns out it was way too simpler. are you using the default 2d asset platforms? If so, the default 2d asset platforms have an attached collider set to be slippery, just turn that off.
     
  19. AdmiralThrawn

    AdmiralThrawn

    Joined:
    Dec 1, 2013
    Posts:
    18
    I'd like to add my case just in case another developer has a similar problem like me:

    If you have a gameObject with a spherical collider (e.g. with frozen x- and y- axes but the y axis remains unconstrained) and you got a 2d sprite on it which - like in my game project - is facing itself to the camera (via script), then your GO will slide after any movement IF you have no angular drag on the rigidbody component! Setting it to 1 eliminated that problem.

    Here a picture to visualize the problem I had.
     

    Attached Files:

  20. VasileQ

    VasileQ

    Joined:
    Jan 12, 2018
    Posts:
    1
    FancyACuppa likes this.
  21. ultraroar101

    ultraroar101

    Joined:
    Jun 16, 2017
    Posts:
    2
    If you are using a capsule or sphere collider, don't. Perform the movement on a box collider and if you still want to use a capsule/sphere collider afterwards, but ensure that the box collider is always covering the base of the character. I have found that the rotation is due to the friction on the ground.
    Just don't use a capsule collider.
     
  22. GameEverything

    GameEverything

    Joined:
    Feb 22, 2016
    Posts:
    10
    This is the oldest thread ever, lol. Anyway, I fixed the same problem rather quickly by using a physics material on both the ground and player where I then changed the friction on both. I remember having this problem like five years ago and it was reminiscent of the ice levels in almost every old school NES game; Battletoads, Mario 3, etc. Those were the days.
     
  23. ZeldaSpace

    ZeldaSpace

    Joined:
    Jan 21, 2018
    Posts:
    2
    Adding those constraints you show in the picture fixed my problem. Thanks!
     
  24. contact_sachin

    contact_sachin

    Joined:
    Apr 19, 2018
    Posts:
    2
    your plane on which rigidBody is resting has non uniform scale along x and z axis.
     
  25. Samupo

    Samupo

    Joined:
    Dec 2, 2017
    Posts:
    1
    I know this is an old post but it's the first one that appears in Google when you search for the problem so I'm sharing my solution here.

    Quick and easy: put gravity scale to 0 when the object is grounded, reset it to your like whenever it's not.

    This has worked for me in 2D, haven't tested in 3D yet.

    To find if the object is grounded I get the collisions and check normals, I'm using y > 0.5f because I don't want any sliding. I keep a list of all the floors the object is colliding with, adding and removing them as the object enters or leaves the collisions. When no floors are present I just assume it's either jumping or in free fall and reset gravity.
     
    ShamansDock and sourbacon like this.
  26. Aeroxima

    Aeroxima

    Joined:
    Jul 19, 2016
    Posts:
    15
    Alright so I just spent so many hours on this today and nothing above helped, but I figured out what my problem was:

    I just had to change from using Input.GetAxis() to Input.GetAxisRaw()

    Basically GetAxis is applying smoothing. Went through hell to figure this out, doing all kinds of tests and implementing the same things a ton of different ways, thinking it's crazy how my code has told it to teleport and stop and it keeps sliding after all buttons had been let go.

    I never suspected the part that would be messing it up would be the "direction to move" variable.
     
    BenKiese, sethpunchman and RuteMooth like this.
  27. yoyo696

    yoyo696

    Joined:
    Aug 11, 2015
    Posts:
    14
    Best Answer was increasing the linear drag. Thx <3
     
  28. jaredgregersen

    jaredgregersen

    Joined:
    Nov 24, 2018
    Posts:
    1
    [¬º-°]¬

    An option that worked for me (for a player controlled 2D object w/rigid body that kept sliding) was to alternate between a speed variable that is a positive integer value during desired movement, and a value of 0 (when needing to stop without sliding).

    For example, assuming Speed is defined as a positive integer (such as 1, 2, etc...) and used in determining desired movement speed (while checking for key input, in Update() ):



    //Movement
    if (Input.GetKeyDown("a"))
    {
    speedtmp = Speed;
    ...



    //Movement Stop
    if (Input.GetKeyUp("a"))
    {
    speedtmp = 0;
    ...


    The variable speedtmp can then be used in conjunction with the rigid body .MovePosition method, within FixedUpdate(), for determining rigid body position:

    rigidBody.MovePosition(rigidBody.position + inputs * speedtmp * Time.fixedDeltaTime);


    When speedtmp = 0 , this results in the above line of code also equaling 0, stopping the rigid body position at the current position (without sliding further).
     
  29. NerdIt-

    NerdIt-

    Joined:
    Oct 19, 2018
    Posts:
    24
    I realize this question was posted in 2009, but for any one that needs help, I tried playing with the Angular Drag on the Rigidbody component. I set the value to 100 and it fixed my issue. Hope I help someone.
     
    Favour001 likes this.
  30. shwa

    shwa

    Joined:
    Apr 9, 2012
    Posts:
    461
    Same problem here. Trying lots of fixes. Still sliding.

    This is inane.

    Unity should chime in and offer a solution, asap.
     
    Riancroc1, parkrain1189 and Favour001 like this.
  31. pokruchin

    pokruchin

    Joined:
    Apr 24, 2019
    Posts:
    38
    This is the only answer, that solved my issue for 95%. But in my case I had to set not 10 or 15 but 50.
    Thank you from 2019!
     
    Mahdirezaei and Favour001 like this.
  32. mikeKB

    mikeKB

    Joined:
    Jan 7, 2018
    Posts:
    1
    If you're not planning on having any outside forces moving the player horizontally (explosions, sloped angles where the player slides, etc.), you could do something similar to:


    Code (CSharp):
    1. if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D)) {
    2.     rb.velocity = new Vector2(0, rb.velocity.y);
    3. }
     
  33. copperhaxor55

    copperhaxor55

    Joined:
    Jul 15, 2018
    Posts:
    2
    Code (CSharp):
    1.     public void Move()
    2.     {
    3.         playerGameObject.transform.forward = virtualJoystick.heading;
    4.         thisRigidBody.transform.forward = virtualJoystick.heading;
    5.         thisRigidBody.position += virtualJoystick.rightMovement * playerMovementSpeed * Time.deltaTime;
    6.         thisRigidBody.position += virtualJoystick.upMovement * playerMovementSpeed * Time.deltaTime;
    7.     }
    This was triggered by the following (causing a slide, regardless of what settings i put in rigidbody)

    Code (CSharp):
    1.         if (virtualJoystick.heading != null)
    2.         {
    3.             Move();
    4.  
    5.         }
    Changed to this and now works.

    Code (CSharp):
    1.         Vector3 MoveVector = virtualJoystick.heading;
    2.         if (MoveVector != Vector3.zero)
    3.         {
    4.             Move();
    5.         }
     
  34. RuteMooth

    RuteMooth

    Joined:
    Feb 24, 2018
    Posts:
    1
    Thank you very much, this helped me alot! :D But i spent many hours, too. :(
     
    sethpunchman and Aeroxima like this.
  35. leglegarmhead

    leglegarmhead

    Joined:
    Nov 10, 2019
    Posts:
    4
    I'll just add on to this, there's so many solutions but just turning off GravityScale (setting it to 0) for the sliding object worked for me.
     
  36. michaelday008

    michaelday008

    Joined:
    Mar 29, 2019
    Posts:
    135
    After struggling with this for several hours here was how I fixed the issue:

    Code (CSharp):
    1. rigidBody.interpolation = RigidbodyInterpolation.None;
    2. rigidBody.detectCollisions = false;
    3. rigidBody.isKinematic = true;
    It seems that Unity has a bug (or unwelcome "feature") that turning your rigidbody to kinematic still allows it to experience physics unless RigidBodyInterpolation is set to None. Even completely disabling colliders on your component does not help.

    The worst part is the failures are silent and I only discovered this after desperately trying every single setting.
     
  37. Deleted User

    Deleted User

    Guest

    I have a rigidbody with a mass set to 15 and this problem occurred to me, I just set my linear drag from 0 to 30 and that fixed it.
     
  38. Nuntis

    Nuntis

    Joined:
    Nov 18, 2019
    Posts:
    1
    you could remove the velocity from the player.

    if(Input.GetKeyDown(KeyCode.D))
    {
    Vector2 playerVelocity = new Vector2(5f, myRigidbody.velocity.y);
    myRigidbody.velocity = playerVelocity;
    myAnimator.SetBool("IsRunning", true);
    }
    else
    {
    Vector2 playerVelocity = new Vector2(0f, myRigidbody.velocity.y);
    myRigidbody.velocity = playerVelocity;
    myAnimator.SetBool("IsRunning", false);
    }
     
  39. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    With the new unity 2019.3.1f1, the detectCollisions option is gone.

    The "Edit > Project Settings > Physics > Solver Iteration Count:" is also gone. How would I go about stopping my kinematic rigidbody from sliding nowadays?
     
  40. iRSS

    iRSS

    Joined:
    Mar 27, 2020
    Posts:
    9
    You can try to stop:
    Code (CSharp):
    1. rb.constraints = RigidbodyConstraints2D.FreezeAll;
    For move again:
    Code (CSharp):
    1. rb.constraints = RigidbodyConstraints2D.FreezeRotation;
     
    MertArdal likes this.
  41. team_eden

    team_eden

    Joined:
    May 27, 2014
    Posts:
    8
    Hello everyone!!

    Although, I do not post much, please allow me to submit the
    following.

    ## 'The natural physics of Unity, are very 'slidy,' help!? ##

    How do I eliminate Unnecessary sliding?

    Remember also, sliding can be 'good' too!
    Like jumping, or projectiles!! Very slidy.. yes.

    Well...

    if(player_moving)
    { player_rigidbody.drag=reasonableValue_forSliding; }
    else /*player 'is not' moving*/
    { player_rigidbody.drag=reasonableValue_forStanding;}

    This can be justified, as (people/living things)
    have a tendency to 'seek ground,' aka drag,
    aka friction & center of gravity.

    public float dragForce_standing=10.0f;
    // consider that this should always be greater than..
    public float dragForce_moving=1.0f;
    // something already in motion (inertia)
    // would have less 'drag,' if it were applied
    // this way

    In terms of 'drag,' then, more force is applied, as less motion is happening; you might call this
    behaviour, sort of, 'clingy.'

    Anyway,

    It appears that the default weight unit is kg, so my 102 kg
    object, was easily calculated. Slide, not so much.

    The end values were very high, for my purposes. So, do not
    be afraid to reach 4, or even 5 digits. However, be true
    to the 'weight' of your object, always.

    I will not give specific settings, that's part of the fun. Be reasonable,
    and pay attention, when adding force to the player.


    Thank you.

    -TE
     
  42. team_eden

    team_eden

    Joined:
    May 27, 2014
    Posts:
    8
    Here

    I came up with this today
    (Reposted with 'drag over time')

    Enjoy! :)


    void preWalking()
    { sphere_rigidbody.drag=drag_moving;
    drag_baseMult_current=0.0f;
    drag_standingMult_current=0.0f;
    }

    public float drag_base=1.0f; // minimumSlideDrag
    public float drag_multiple=0.01f; // multiple calc for base drag
    public float drag_baseMult_current=0.0f;
    public float drag_standingMult_current=0.0f;

    void motionState()
    { if(!keypressed)
    { keypressed=false;
    player_animation.speed=2.0f;
    player_animation.SetInteger("animation",0);

    if(!isGrounded)
    { sphere_rigidbody.drag=drag_moving; // regular motion drag
    drag_baseMult_current=0.0f;
    drag_standingMult_current=0.0f; }

    else
    { if(drag_baseMult_current<1.0f) // increase drag, after releasing it
    { drag_baseMult_current+=drag_base*drag_multiple;
    sphere_rigidbody.drag=drag_moving*(1.0f*drag_baseMult_current); }

    else
    { Debug.Log("'Standing Current");

    if(drag_standingMult_current<1.0f) // increase drag, after releasing it
    { drag_standingMult_current+=drag_base*drag_multiple;
    sphere_rigidbody.drag=drag_standing*(1.0f*drag_standingMult_current); }
    }
    }
    // Debug.Log("key not pressed");
    }
    }
     
    Last edited: May 7, 2020
  43. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    This is for me the best answer so far, the only thing is because is in 2D you usually default froze the z rotation when you reactivate the rigidbody2D, you have to reference it twice (one for unfroze all and one to froze the z-axis).
    Also, doing this how will affect the performances?
     
  44. WaveLink

    WaveLink

    Joined:
    Dec 25, 2019
    Posts:
    2
    Litteraly just add a flat box collider to the bottom if you are using a capsule player. The rigidbodies dont like curved surfaces
     
    rc82 likes this.
  45. rc82

    rc82

    Joined:
    Jan 28, 2020
    Posts:
    44
    This worked!!! Thank you!
     
  46. Zolaire

    Zolaire

    Joined:
    May 23, 2020
    Posts:
    21
    Increasing the Linear Drag and Angular Drag worked for me.
    Just imagining that a single bug has survived for 11 years is insane, this thread is from 2009.
    Damn..
     
  47. lfagel84

    lfagel84

    Joined:
    Jan 1, 2021
    Posts:
    1
    Where is linear drag?
     
  48. Zolaire

    Zolaire

    Joined:
    May 23, 2020
    Posts:
    21
    On the rigidbody
     
  49. Rassepoju

    Rassepoju

    Joined:
    Feb 21, 2021
    Posts:
    1
    All of the answers, this was the answer that made everything work. Thanks
     
    MrGameAndCreate likes this.
  50. YotamAdi

    YotamAdi

    Joined:
    Aug 8, 2021
    Posts:
    1
    Yep like Aeroxima said just change Input.GetAxix() to Input.GetAxisRaw().
    Now its shuld work perfectly
     
Thread Status:
Not open for further replies.