Search Unity

Tiny inexplicable movement on objects using Rigidbody.velocity

Discussion in 'Physics' started by Kaart, Jan 22, 2019.

  1. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    Objects in my game using a Rigidbody that are moved by setting the velocity have tiny movement all the time.

    upload_2019-1-22_20-25-22.png

    upload_2019-1-22_20-25-35.png

    As you can see from it's position in the screenshots above it's slightly moving around.
    The object is a cube using a Box Colider and is resting on another perfectly straight cube also using a box collider.

    upload_2019-1-22_20-27-56.png

    I'm using a very basic script to move around the cube. The problem occurs even when there is no input at all. The problem doesn't occur when I use Rigidbody.AddForce to move the object.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CubeController : MonoBehaviour
    4. {
    5.     Rigidbody _rigidbody;
    6.     float _horizontalInput, _verticalInput;
    7.     public float _moveSpeed = 10.0f;
    8.  
    9.     void Start()
    10.     {
    11.         _rigidbody = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         _horizontalInput = Input.GetAxisRaw("Horizontal");
    17.         _verticalInput = Input.GetAxisRaw("Vertical");
    18.     }
    19.  
    20.     private void FixedUpdate()
    21.     {
    22.         Vector3 movement = new Vector3(_horizontalInput, 0, _verticalInput) * _moveSpeed;
    23.         movement.y = _rigidbody.velocity.y;
    24.         _rigidbody.velocity = movement;
    25.     }
    26. }
    Does somebody know why the cube is moving around all the time?
     

    Attached Files:

    Last edited: Jan 22, 2019
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    There are options in Edit menu Project Settings -> Physics, which should allow you to set sleep threshold.
    Tried play with these?
    upload_2019-1-22_19-34-43.png
     
  3. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    Thanks for your reply. I tried making it really low and really high but it seems to have no effect.
     
  4. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    For some reason Rigidbody.Velocity is not the same as movement although I set it in FixedUpdate():

    upload_2019-1-22_20-47-18.png
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    What about playing with drag?
    Oh btw. Is your movement absolute 0? Did you try measure it with Debug.Log with high precision?

    Edit: ignore second question, you responded before me :)
    Edit2: Do you have only one script, affecting this object?
     
  6. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    I suspect gravity has something to do with it. If I set gravity.y to zero in Project Settings > Physics or uncheck Rigidbody.UseGravity the movements stops.
    upload_2019-1-22_20-50-59.png

    upload_2019-1-22_20-51-46.png

    I tried playing with Drag but something weirds happens here. If Drag >= 50 the movement stops, however I'm not able to move the object at all. Even when I increase it's velocity to a really high value. I Drag < 50 the movement continues.
     

    Attached Files:

  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Drag of 50 is rather big.
    A bit weird.
    But what you can try also, is use physics materials, to add friction.
    Otherwise objects tend to skid.
     
  8. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    The object is using a physics material. The object still moves around even if it did not receive any input at all. There is no other script on the object.
     
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Which Unity version are you using?
    Can you try different version? Probably earlier and stable.
     
  10. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    I'm using Unity 2018.3.0f2 Personal.

    Edit: now updating to Unity 2018.3.2f1.
     
  11. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    Try using Rigidbody.AddForce(velocityDifference, ForceMode.VelocityChange) instead of modifying Rigidbody.velocity directly. You may calculate velocityDifference as "desired velocity" - Rigidbody.velocity.
     
    Antypodish likes this.
  12. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    Thanks for your reply. This removed the tiny constant movement. But I'm still curious why setting the velocity of the Rigidbody directly causes this movement.

    Even when I do something like: Rigidbody.Velocity = New Vector3(0.0f, 0.0f, 0.0f) there is still constant movement on the object.
     
  13. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    That's because the Rigidbody is also computing and applying its own velocity on top of yours. Simplifying things a lot, this is what happens in each physics simulation step:
    1. FixedUpdate called
    2. The acceleration is computed out of all forces affecting the rigidbody (gravity, collisions, AddForce, etc)
    3. The velocity is updated using the current velocity (v0) and the acceleration: v = v0 + a*dt
    4. The position is computed out of the velocity: pos = pos0 + v*dt
    The key for the behavior you're observing is in the step 3. You've modified the velocity parameter in FixedUpdate, which means that you've set v0 explicitly. But then Unity computes a new velocity v using yours as initial velocity but still applying all other forces.

    Using AddForce means that the acceleration computed in step 2 includes the velocity change you've specified and assumes the effects of the other forces. So when this acceleration is applied to the current velocity in step 3 results in the desired velocity finally applied to the rigidbody.
     
    Last edited: Jan 28, 2019
    KaroBm and Kaart like this.
  14. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    Thanks for your help! I never had any trouble using velocity with Rigidbody2D's but your
    explanation makes sense.
     
  15. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    I did some further investigation and the problem gets even stranger.

    It only happens when the ground is also a cube that is rotated 90 degrees (so objects are resting on it's side instead of the top). When I change the rotation back to 0 degrees the problem does not occur...

    So to sum it all up. Tiny movement occurs in objects with a Rigidbody that use Rigidbody.Velocity to move around but only when the ground is a Cube that is rotated 90 degrees...
     
  16. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    2D physics use a different physics engine (Box2D). Possibly it has a different logic.

    Physics mysteries :D