Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Rigidbody2D ForceMode.Impulse

Discussion in '2D' started by RockForest, Nov 25, 2013.

  1. RockForest


    Jul 4, 2012
    Seems like rigidbody2D lacks the functionality of forcemode.
    Is there any way to simulate that effect or have i missed a feature somewhere?

  2. gfoot


    Jan 5, 2011
    For forces:

    If the value you are applying is an impulse, then calculate a force by dividing your impulse by the time delta (usually Time.fixedDeltaTime).

    If the value you are applying is an acceleration, then calculate the force by multiplying your acceleration value by rigidbody2d.mass.

    If the value you are applying is a velocity difference, then do both - i.e. multiply by rigidbody2d.mass / Time.fixedDeltaTime.

    Code (csharp):
    2. Vector2 ApplyForceMode(Vector2 force, ForceMode forceMode)
    3. {
    4.     switch (forceMode)
    5.     {
    6.         case ForceMode.Force:
    7.             return force;
    8.         case ForceMode.Impulse:
    9.             return force / Time.fixedDeltaTime;
    10.         case ForceMode.Acceleration:
    11.             return force * rigidbody2d.mass;
    12.         case ForceMode.VelocityChange:
    13.             return force * rigidbody2d.mass / Time.fixedDeltaTime;
    14.     }
    15. }
    For torques I'm not sure what is correct because it's not clear what Unity uses for the inertia tensor (the angular equivalent of mass). Still, you can apply an angular impulse easily enough, by dividing by Time.fixedDeltaTime. For the other two ForceModes you could just modify rigidbody2d.angularVelocity directly and get the right effect - i.e. for VelocityChange just add your delta onto the existing velocity, and for Acceleration add the acceleration value multiplied by Time.fixedDeltaTime.
    domiii likes this.
  3. jrdata2k


    Oct 6, 2008
    ..even better you could turn this into an extension method, for those that don't know this needs to be added to a static class to work and will actually add the missing method to the rigidbody2D component. I've modified the method to actually apply the force rather return it ie. it more closely matches the rigidbody(3d) AddForce method:

    Code (csharp):
    1. public static void AddForce ( this Rigidbody2D rigidbody2D, Vector2 force, ForceMode mode ) {
    2.         switch ( mode ){
    3.         case ForceMode.Force:
    4.             rigidbody2D.AddForce( force );
    5.             break;
    6.         case ForceMode.Impulse:
    7.             rigidbody2D.AddForce( force / Time.fixedDeltaTime );
    8.             break;
    9.         case ForceMode.Acceleration:
    10.             rigidbody2D.AddForce( force * rigidbody2D.mass );
    11.             break;
    12.         case ForceMode.VelocityChange:
    13.             rigidbody2D.AddForce( force * rigidbody2D.mass / Time.fixedDeltaTime );
    14.             break;
    15.         }
    16.     }