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

Resolved Rigidbody.GetAccumulatedForce()

Discussion in 'Physics' started by ImprobableDoge, Sep 14, 2023.

  1. ImprobableDoge

    ImprobableDoge

    Joined:
    Aug 4, 2021
    Posts:
    40
    Hi, could anyone please explain me the Rigidbody.GetAccumulatedForce() method? I quite didn't understand the documentation and I can't get it to work with my game. Thanks!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    When you add a force, it doesn't get added there and then but is added to a sum. When the simulation runs, it's time-integrated into velocity and that sum is reset. At any point you can ask what the current accumulated force is.

    As per the simple code example in the doc, it adds a force, reads what the current force total is then adds the same but negative, effectively subtracting it and cancelling and force that was previously added.
     
  3. ImprobableDoge

    ImprobableDoge

    Joined:
    Aug 4, 2021
    Posts:
    40
    If so, the script I'm using should be working properly... I have a moving platform that is moved by adding forces in the FixedUpdate() method, and in another script I try to use the accumulated forces also in FixedUpdate(), to apply it to whatever rigidbody object is on top. The value debugged was Vector3.zero. Why doesn't this work?
     
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,449
    It seems to be a way to simulate a rigidbody slowing down on its own but why:

    Code (CSharp):
    1.         rigidbody.useGravity = false;
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    It's such a simple call that just reads the accumulator of forces you've asked to add. Beyond that, it'll be whatever else you're doing causing you an issue. Don't make it difficult by applying a force in one script then trying to do something in another, potentially in the wrong order? You can easily test this in a simple script as per the example so you understand first. Walk before you run and all that. ;)

    Ever wondered where the force you're asking to add when calling AddForce is stored? Here. It's allowing you to read it. If you thought that calling AddForce as a force immediately did something to the velocity then that's incorrect.

    It's not a "way to simulate a rigidbody slowing down". It doesn't have any active role in anything. It's just a read-only value which you can use for whatever you need; it just tells you what you've added so far since the last simulation step used it and cleared it.

    Add a force of (10, 0, 0) then you'll read (10, 0, 0). Add another force of (1, 1, 1) and you'll read (11, 1, 1). After the simulation step, you'll read (0, 0, 0). It couldn't be more simple really.

    Why turn off gravity in the example? Because the example is showing you that it's stopping the user force added in the example from having any effect on velocity by reading it and apply the inverse of it, effectively cancelling it out. Gravity would get in the way of that example right? Gravity is not a user force, in-fact it's not a force at all, it's an impulse as are all of the collision contact and joints responses. The only forces are the ones you specify, time-integrated into velocity for you by the engine.

    In 2D we have:
    https://docs.unity3d.com/ScriptReference/Rigidbody2D-totalForce.html
    https://docs.unity3d.com/ScriptReference/Rigidbody2D-totalTorque.html

    Note, I'm not a 3D physics dev so I only noticed this after reading the post above and looked at the docs myself.

    Hope that rambling explanation helped a little! :p
     
  7. ImprobableDoge

    ImprobableDoge

    Joined:
    Aug 4, 2021
    Posts:
    40
    Turns out I'm a silly, I tested out things individually and you were right. The issue wasn't even in the method, I was trying to get the accumulated forces from a different game object. I'm such a goober. Thanks, now I understand it :p
     
    MelvMay likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Good stuff, glad it's working for you. I was starting to doubt it worked.... ;)
     
  9. Anikate25

    Anikate25

    Joined:
    Sep 17, 2023
    Posts:
    3
    Well! when you apply a force to an object using Rigidbody, add force that force isn't immediately enacted but rather tallied up over time. It's as if you're keeping a running count of all the forces acting on the object. As your game progresses, these accumulated forces are integrated over time to calculate the object's velocity.
    At any moment in your game, you can check this cumulative force total using Rigidbody. A practical example would be applying a force to move an object forward and then using GetAccumulatedForce to see how much force has built up. If you apply the same force in the opposite direction (making it negative), you effectively subtract it from the total, effectively canceling out any prior forces.
     
    Last edited: Sep 24, 2023
    Bunny83 and APSchmidt like this.