Hey All,
I’m trying to Add Force to a physics object via a coroutine. However, I’m getting inconsistent force added, so must be doing something wrong. *Note: I use a 3rd party character controller so I call AddMomentum() rather than rb.AddForce(). But it does a very similar function.
Why doesn’t this code add the same amount of force every time? I’m calling it once from within the FixedUpdate loop. Appreciate any advice!
Thx!
IEnumerator MomentumAdder(Vector3 _dir, float _duration, AnimationCurve _curve)
{
// Convert duration of momentum to be added into 0-1 so we can apply it against the animation curve
float step = 1 / _duration;
float curveTime = 0f;
float durationCountUp = 0; // We count this up in our while loop each fixedupdate
// while within duration time
while (durationCountUp < _duration)
{
// Adjust the Vector3 force direction based on curve
_dir *= _curve.Evaluate(curveTime);
// Countup the duration adjusted for fixedDelta time (as we want this to run on the physics clock)
durationCountUp += Time.fixedDeltaTime;
// Get the next place on the curve based on where we are in the duration
curveTime += durationCountUp * step;
// Add Momentum
AddMomentum(_dir);
// Yield until the next physics step
yield return new WaitForFixedUpdate();
}
}