Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

RigidBody -> 0 velocity -> still moving?

Discussion in 'Physics' started by _watcher_, Apr 9, 2016.

  1. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    259
    Hi,

    id like to stop the movement of a rigid body affected by gravity.

    Test1: set gravity to 0 (Physics.gravity.y = 0 or something like that)
    Result: rigid body isnt moving

    Test2: keep gravity. Set RigidBody.velocity to Vector3 (0,0,0) in onFixedUpdate.
    Result: Rigid body is moving ever so slightly downwards.

    My question is, how can i stop RB movement while keeping gravity settings intact?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,418
    Gravity is an acceleration. You might set the velocity to zero, but the acceleration moves it slightly on each physics step before you actually reset the velocity to zero again.

    In addition to setting velocity to zero, you must cancel the action of the gravity in that rigidbody. You can do so like this:

    Code (CSharp):
    1.  
    2. void FixedUpdate ()
    3.    {
    4.    GetComponent<Rigidbody>().AddForce(-Physics.gravity, ForceMode.Acceleration);
    5.    }
    EDIT: Note that this has the same exact effect as disabling "Use Gravity" in the Rigidbody component. There's no difference on using the above code or disabling gravity in the rigidbody.
     
    Last edited: Apr 11, 2016
  3. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    259
    Thank you for the reply! I cant test this atm, as i worked around the problem, but this will definitely help in the future!
     
  4. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Just curious why not just set the gravity off? It would be much more performance efficient.
     
  5. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    259
    @RavenOfCode can you turn off gravity for that specific RB? because other RBs are also using gravity..
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,418
    I don't think there's a difference in performance here. All the gravity does is:
    Code (CSharp):
    1. AddForce(Physics.gravity, ForceMode.Acceleration);
    Adding an exact opposite acceleration just cancels the one set by gravity. All forces/accelerations are combined (summed) and the resulting single force is applied to the rigidbody on the next physics update.

    So disabling gravity is just a more convenient way (that's what the rigidbody.useGravity property is for), but not a significant performance improvement.
     
    _watcher_ and RavenOfCode like this.
  7. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Yeah, just grab that particular rb and turn it off.
    Code (CSharp):
    1. GetComponent<Rigidbody>().UseGravity = false;
     
    aihodge and _watcher_ like this.
  8. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    259
    So that method actually exists.. Kappa. Thank You.
     
    RavenOfCode likes this.