Impulse force time

Ok, I couldn’t formulate this question simple enough to google it effectively, so I hope I can get some help here.

I have an object that I add force to using Rigidbody2D.AddForce with ForceMode2D.Impulse as a parameter. Now the question is, is it possible to calculate time in which this force will be affecting my velocity if I know starting velocity, mass and everything else I can get from RigidBody2D?

I need to calculate this using only starting parameters as the actual velocity can change by other means as well.

The in-game situation is: I have a character that performs ‘dash’. I do this with AddForce and it works great! But I need to know for how long the character is dashing so things can happen in that period of time. Any collisions or actions that happen along the way are irrelevant for me, I only need the time.

Right now I just approximated the time by dividing the force I add (usually around 50) by 100 and it looks quite accurate for small forces, but I don’t really like this solution.

First, the easy (and recommended) way out

Consider making your character have a set dash time (or dash distance) to begin with. Save the initial magnitude of their velocity, for instance, and when the dash is over, set the magnitude of their current velocity back to the previous magnitude or their current, whichever is smaller. This allows the direction to still change freely in-dash.

Furthermore, I highly recommend this method over forces because you maintain more explicit control over how the character dashes. This means that balancing is easier, and it opens the door for easily adding modifiers, different character stats, status effects, etc. It also makes it much more intuitive for you to edit. The physics engine is great for newtonian physics, but most often a character is not subject to these same laws- a dash might be the result of the character kicking off with their feet inhumanly fast, or something much more bizzare, like magic or some sort of sci-fi gadget, and regardless, nothing in real life really dashes the way a game character should. Trying to approximate a dash using forces, and finding the length, is like shooting a gun in space and asking how long before the bullet slows down.

Although, I understand that this is not actually an answer to your question, so if you still want to do what you are doing, it’s important to quickly look at:

The physics:

The best way to think about the force modes in my opinion is to think about their units. These are documented with the 3D ForceMode, but the principle is the same in 2D.

-Force is in the units of force: Newtons, or a kgm/(s^2).*

This means that every second that AddForce is called, the velocity increases by force/mass.

-Impulse is forcetime, and thus in units of kgm/s.

This means that every time that AddForce is called, the velocity increases by impulse/mass.

This means that by definition Impulse is instantaneous- the force never starts or stops its effect, it simply applies an immediate change. The explicitly written code for adding an impulse is:

rigidbody.velocity += impulse/rigidbody.mass;

Calling this over a period of time doesnt make a lot of sense, because rather than adding accelleration per timestep, the velocity is just being changed immediately. Additionally, the smallest timestep- the framerate- changes. Ordinarily, this would be solved with:

rigidbody.velocity *= (impulse*Time.deltaTime)/rigidbody.mass;

This is, interestingly, the definition of force in the update function. In fixedUpdate, Time.fixedDeltaTime is used, and you end up with the exact same result as ForceMode.Force.

For this reason, the answer for how long an impulse applies is 0. It happens instantly. Now, obviously, this doesn’t predict how long your dash will last, so I have amended this to a slightly different question: How long after an impulse will it take for resistive forces to bring the object back to its original velocity, assuming no other forces are applied. The answer relies quite a bit on what kind of force is resisting motion, however this is usually friction. For most characters, air resistance is negligable and frankly often feels bad anyway. So:

The Calculation:

Assuming a constant resistance R, such as friction, the time in seconds it takes to go from the impulse velocity to the starting velocity is I/R, where I is the impulse applied.

To express this in more physical units, the time is I/(ugm), where g is gravity, m is mass in kg, and u is the coefficient of friction.

Regardless, this will give results that, while they would work, might feel bizzare. I highly recommend the method I provided in the beginning, as it is easier to make a dash feel right that way. Using the physics engine for things that aren’t truly physics never produces the crisp feel that you’re going for.