Search Unity

Rigidbody keeps sliding

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

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

    broskibble

    Joined:
    Aug 25, 2021
    Posts:
    1
    its not there
     
  2. KimberleyC

    KimberleyC

    Joined:
    Mar 22, 2021
    Posts:
    20
    Set the isKinematic property of the rigidbody of the GameObject that's acting as the surface to true; that worked like a charm for me
     
  3. MsMore

    MsMore

    Joined:
    Nov 7, 2020
    Posts:
    1
    I was having the same issue, I was stuck for hours. Hope this helps:

    I have a Player with child FocalPoint to rotate.
    FocalPoint about and it's child is the Maincamera.

    My Player kept sliding even after removing the PlayerController, RotateCamera scripts.

    Turns out the Focal Point had a box collider which was somehow causing it to slide and drag around even in mid air.
     
  4. MaxxRafen

    MaxxRafen

    Joined:
    Apr 7, 2014
    Posts:
    31
    I know this is an old thread, but in my case it was the Collider offset. If it's something other than 0,0 it will cause the object to move on a flat surface.
     
    SwankyOrca likes this.
  5. LordSidious4167

    LordSidious4167

    Joined:
    Jun 5, 2020
    Posts:
    2
  6. xm4str33

    xm4str33

    Joined:
    May 13, 2017
    Posts:
    1
    Here is the solution

    1. Go to your player's rigid body > increase the Drag number
    2. Go to your player's rigid body > Constraints > Freeze Rotation on XYZ and Freeze Position on Y
     
  7. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    So, I recently came up with this issue myself and no amount of freezing rotations, physics settings and all of the above could fix my rigidbodies to sliiiightly sliding away, veeeery slowly.

    I've had enough of this so I just made a script to fix it.
    This script lets you define a minimum allowed velocity for your rigidbody and, if it moves slower than that, it will force the rigidbody to stay in place.
    The script will not interfere will regular movements and will not mess with your rigidbody's acceleration or deceleration.

    This is intended for cases when the rigidbody is mainly stable, but it keeps drifting away very slowly over time.
    It's clear to me that if your object is moving at a rate of 0.5 or 1 millimeter per second, it's drifting away.

    Save this as FixRigidbodySliding.cs and put this script in the rigidbody you want to stop drifting away. Done.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FixRigidbodySliding : MonoBehaviour
    6. {
    7.     [Tooltip("A velocity per axis smaller than this will be considered unwanted sliding and fixed.")]
    8.     [Min(0.0005f)] public float minimumVelocityAllowed = 0.001f;
    9.  
    10.  
    11.     new Rigidbody rigidbody;
    12.     Vector3 lastPosition = Vector3.zero;
    13.  
    14.     void OnEnable()
    15.     {
    16.         rigidbody = GetComponent<Rigidbody>();
    17.         if (rigidbody == null) { this.enabled = false; return; }
    18.         lastPosition = rigidbody.position;
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.         var delta = rigidbody.position - lastPosition;
    24.         var minDeltaPos = minimumVelocityAllowed * Time.fixedDeltaTime * 10f;
    25.  
    26.         // avoid issues when teleporting object:
    27.         if (Mathf.Abs(delta.x) > minDeltaPos || Mathf.Abs(delta.y) > minDeltaPos || Mathf.Abs(delta.z) > minDeltaPos)
    28.         {
    29.             lastPosition = rigidbody.position;
    30.             return;
    31.         }
    32.  
    33.         var vel = rigidbody.velocity;
    34.         var rbp = rigidbody.position;
    35.         var posx = rbp.x;
    36.         var posy = rbp.y;
    37.         var posz = rbp.z;
    38.         if (Mathf.Abs(vel.x) < minimumVelocityAllowed) posx = lastPosition.x;
    39.         if (Mathf.Abs(vel.y) < minimumVelocityAllowed) posy = lastPosition.y;
    40.         if (Mathf.Abs(vel.z) < minimumVelocityAllowed) posz = lastPosition.z;
    41.         rigidbody.position = new Vector3(posx, posy, posz);
    42.  
    43.         lastPosition = rigidbody.position;
    44.     }
    45.  
    46. }
    47.  
    I hope this is useful to others. :)
     
    Last edited: May 26, 2022
    ProGameDevUser likes this.
  8. Veteranfighter

    Veteranfighter

    Joined:
    Jul 3, 2020
    Posts:
    15
    Volumes can also cause this to happen. Check all your objects with box colliders on them.
    Rigidbodies inside Colliders will cause them to gain force to get out of the collider
     
  9. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    wow, all these years and we still have the sliding colliders. I thought this would be fixed by now.
     
  10. gabrielhaitin

    gabrielhaitin

    Joined:
    Jul 20, 2018
    Posts:
    1
    I was making a reach tool added to my FPScontrollers camera which mass was default at 1 , same with my FPScontrollers mass within rigidbody, once i put my FPScontrollers mass at 80 and kept the reach tool at 1 it stopped the sliding.
     
  11. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    what do you mean by reach tool? I didn't understand what you did or how it can help us. Can you attach a screenshot?
     
  12. Hellmiau

    Hellmiau

    Joined:
    Jun 21, 2023
    Posts:
    1
    in my case, the 3D sphere had a box collider instead of a sphere collider.
    Idiotic i know but i just started.
     
  13. RoWh0906

    RoWh0906

    Joined:
    Feb 2, 2015
    Posts:
    2
    Hello everyone,
    Another inspiring dev here from 2023 - using version 2022.2.1f1.

    The issue is still in the engine when you have a lot of colliders and rigidbodies.
    In my case, if I started the game on Stage 1 - Stage 2 and 3 would have their obstacles sliding randomly.
    But if I started from Stage 2 it was working perfectly fine (meaning, just standing still unless you bumped into them) - that means it's not an issue with the set-up but possible with the transition. However, there is nothing in my code that would change physical properties during the loading of the stages or in-between these stages.

    So I just upped the Solver Iteration Count as recommended by TheButtaSauce - and it just resolved itself.

    Wherever you are and whether or not you are still coding, I hope you are having a wonderful day and thank you!
     
Thread Status:
Not open for further replies.