Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rigidbody character bumps/bounces when moving

Discussion in 'Scripting' started by RalliantoDeLaVega, Jan 30, 2014.

  1. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    Hey, i'm playing around when some movement for at character, i'm using a rigidbody, and i'm moving the charater around, only in the X and Y axis, on boxes with box colliders. I've build the world up from 1x1x1 boxes. When i move the character sideways, he sometimes bump / bounces / jumps a little bit into the air, i think its because he bumps into the corner of a box, even that the boxes are precise next to each other. If i make a long box, he moves smooth, but the idea is to make the world up from boxes. The movement script looks like this:

    Code (csharp):
    1.  
    2. var distToGround: float;
    3.  
    4. function Start () {
    5.     // get the distance to ground
    6.     distToGround = collider.bounds.extents.y;
    7. }
    8.  
    9. function IsGroundedLeftSide(): boolean {
    10.     return Physics.Raycast(transform.position + Vector3.left*0.3 , -Vector3.up, distToGround );
    11. }
    12.  
    13. function IsGroundedRightSide(): boolean {
    14.     return Physics.Raycast(transform.position + Vector3.right*0.3 , -Vector3.up, distToGround );
    15. }
    16.  
    17. private var moveForce : float = 4;
    18. private var jumpForce : float = 5;
    19. private var gravity : float = 10;
    20. private var moveDirection : Vector3 = Vector3.zero;
    21.  
    22. function FixedUpdate () {
    23.  
    24.     //Constant get move direction
    25.     moveDirection = Vector3 ( Input.GetAxis ("Horizontal"), 0, 0 );
    26.     moveDirection = transform.TransformDirection( moveDirection );
    27.     moveDirection *= moveForce;
    28.  
    29.     var velocityChange = (moveDirection.x - rigidbody.velocity.x);
    30.  
    31.     //Movement in X-axis
    32.     //rigidbody.AddForce (velocityChange,0,0 , ForceMode.VelocityChange);
    33.     rigidbody.velocity.x = moveDirection.x;
    34.  
    35.     // Gravity //
    36.     rigidbody.velocity.y -= gravity * Time.deltaTime;
    37. }
    38.  
    39. function Update () {
    40.     // ----- Check for touching the ground ----- //
    41.  
    42.     var down = transform.TransformDirection (Vector3.down);
    43.  
    44.         if ( IsGroundedLeftSide() || IsGroundedRightSide() ) {
    45.             //print ("I touch ground!");
    46.             gameObject.renderer.material.color = Color.blue;
    47.  
    48.             if (Input.GetButtonDown("Jump")) {
    49.                 //rigidbody.AddForce ( 0, jumpForce * rigidbody.mass, 0 );
    50.                 rigidbody.velocity.y = jumpForce;
    51.             }
    52.         }
    53.  
    54.     // ----- When NOT touching the ground ----- //
    55.  
    56.         if ( !IsGroundedLeftSide()  !IsGroundedRightSide() ) {
    57.             //print ("I DO NOT touch the ground !!");
    58.             gameObject.renderer.material.color = Color.red;
    59.         }
    60. }
    61.  
    I should mention the character is frozen in postion Z and roation X/Y/Z and gravity is controlled by script.
    Also, when he lands from a jump, he sometimes bounces back into the air just a little bit, but i want him he stay in his position in the Y axis when i touches ground.

    Somebody have an idea/solution? //If you dont understand the question/problem, ask, and i will try to explain myself better.
    Thanks, Rasmus
     
  2. Tee_Pee

    Tee_Pee

    Joined:
    Aug 18, 2013
    Posts:
    54
    Every modification such as this needs be time based rather than frame based (since frames aren't uniform). All of your velocity modifications need to be multiplied by Time.deltaTime.
     
  3. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    Okay, i multiplied the velocity modifications with Time.deltaTime, only resulting in i had to increase the moveForce significantly, but he still bounces when i move him :/ ?
     
  4. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    Tried everything, looked everywhere..
    I just want a simple physics based movement for at 2D character. I have a box that is frozen in rotation: X,Y,Z and in position Z. It has a rigidbody attached. I move it with rigidbody.velocity = "something".. When i move it over a series of boxes, it collides with the edges, and bounce up a bit, resulting in character not being grounded and unable to jump. What to do ?

    Alternatives:
    use transform.Translate for movement = Very bad collision
    use character controller = cannot change shape from capsule to box, so character has a round bottom, resulting in ugly movement.
    What to do =?
     
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    This is an issue with ghost vertices. You can't build your level tile based using cube colliders on a character that is also a cube. It causes intersection check errors when crossing over the parts that meet, due to one thinking pushing you upwards is the quickest way out of the collider, while the one in front thinks pushing you backwards is the quickest.

    To avoid this, either use polygon/planes as colliders for your surface, or switch to the new 2D system and use EdgeCollider 2D's to make up your surface (this is preferable, since you can then easily define each side as a different layer, for various collision detections)
     
    StrawBox likes this.
  6. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    Thx, i solved the problem with having one huge block with a collider, but still thats a lot of extra work. 2D rigidbodies falls trough both edgeCollider and polyCollider.. And having single polygons instead of cubes dosent solve the problem ;)
     
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Your 2D rigidbody shouldn't fall through it if you're using a BoxCollider2D on it. You should only be using 2D colliders with your 2D rigidbody.
     
  8. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    Okay, in case anyone anyone should stumble upon this very same issue, it can be solved by Edit -> Project Settings -> Physics -> Min penetration for penalty and set it to zero. Now i can move my rigidbody character around by manipulating it's velocity, and use unitys 1x1x1 cubes as tiles for a 2D/3D game
     
  9. JRP25

    JRP25

    Joined:
    Apr 2, 2014
    Posts:
    1
    Thanks!
     
  10. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    nice one, this was bothering me the last coulple of days, tried edge collider at the feet and alls sorts to try and get a smooth movement using velocity.

    this works a treat so far, now i can use my tiled platforms together and build a scene as I go with just the prefabs.

    trying to redo manic miner to learn a bit about 2D stuff :)

    cheers guys, good read..
     
  11. Stevepunk

    Stevepunk

    Joined:
    Oct 20, 2013
    Posts:
    205
    This solution stops the bouncing when moving from one collider to the next, but now my character bounces constantly when not moving.

    I'm using a capsule collider.
     
  12. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
  13. Antonio_Ketch

    Antonio_Ketch

    Joined:
    Nov 3, 2014
    Posts:
    11
    Great Thanks
     
  14. BradDonohoo

    BradDonohoo

    Joined:
    Apr 18, 2015
    Posts:
    4
    FIXED: - Go to Edit > Project Settings > Physics and raise "Default Solver Iterations" and
    "Default Solver Velocity Iterations" - had to raise mine to 12 and 24 - now collisions have never been so smooooth. :p
     
    Antonio_Ketch likes this.
  15. RandomiaGaming

    RandomiaGaming

    Joined:
    Apr 3, 2019
    Posts:
    12
    thank you so much i have been fighting with this for several months now and it just worked first try!!! Yeah!
     
  16. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    719
    omg, this is a game changer

    note, its called Default Contact Offset now
     
  17. BURRIT0S

    BURRIT0S

    Joined:
    Jul 28, 2019
    Posts:
    1
    OMG! Thank you so much!

    In newer versions, it's called Default Contact Offset and you can't set it to zero you have to put in a small decimal like 0.000000001.
     
    the_real_ijed and superwick like this.
  18. Deleted User

    Deleted User

    Guest

    this doesn't work for me. I'm using a tilemap collider 2d and it still bounces when moving even after changing what you said. my tilemap colliders is using 0.1 as it's default scale though. Is this the problem???
     
  19. michaelday008

    michaelday008

    Joined:
    Mar 29, 2019
    Posts:
    135
    I had this problem also in Unity 2020.3.18f1, and I noticed that I had to change two settings for my dragon with a capsule collider running around at 20m/s.

    Set Default Contact Offset to 0.0001 and make sure the rigidbody has the collision mode set to ContinuousDynamic.

    Changing the Default Contact Offset didn't make much difference if I had the rigidbody mode set to ContinuousSpeculative.
     
  20. LordCail07

    LordCail07

    Joined:
    Sep 5, 2018
    Posts:
    2
    In 2020.3LTS, above solution reduced the bumps but didn't eliminate them for me.

    Using following settings AND applying a default Physic Material to both the Player and the Ground object colliders seems to have fully fixed it.

    Project Settings > Default Contact Offset: 0.0001
    Player RigidBody Collision Mode: ContinuousDynamic

    This was totally bugging me. Thank you so much for sharing the solution!
     
  21. dimonic42

    dimonic42

    Joined:
    Feb 25, 2023
    Posts:
    1