0
\$\begingroup\$

I'm looking for some help with smoothing between an original float and a target float, where the original float is consistently changing (potentially interfering with interpolation techniques and or Mathf.SmoothDamp)

I tried Mathf.SmoothDamp with the below:

(float rocketBoostPower is a global variable)

float yVelocity = 0.0F;
float tempForce = rocketBoostPower + Input.GetAxis("Mouse Y") * shiftSpeed / 5f;
rocketBoostPower = Mathf.SmoothDamp(rocketBoostPower , tempForce, ref yVelocity, 0.5f);

This doesn't change the force at all however, it just stays locked in place.

I'm currently using the below to change my power, however I want to smoothly change the power instead of just incrementing sharply on each Update frame:

rocketBoostPower += Input.GetAxis("Mouse Y") * shiftSpeed / 5f;

Please can someone help? I need the assumption that every update will receive a new rocketBoostPower value, whilst still smoothing its previous value.

\$\endgroup\$
1
  • \$\begingroup\$ Make yVelocity a field. \$\endgroup\$ Commented Aug 2, 2017 at 14:14

1 Answer 1

2
\$\begingroup\$

Use a lerp (linear interpolation) instead. It's used as so:

float f = Mathf.Lerp(a,b,t);

f will be set to a "blend" of a,b based on t. When t = 0, f = a. When t = 1, f = b. You can take advantage of this to create some nifty smooth transition functions.

The easiest is just:

rocketBoostPower = Mathf.Lerp(rocketBoostPower , tempForce,t);

Which brings rocketBoostPower closer to tempForce by t "percentage" every time it's called.

Note: because you're blending from and setting to the same var, rocketBoostPower will NEVER equal tempForce unless t = 1.


https://en.wikipedia.org/wiki/Linear_interpolation here's a wiki article for further reading. Interpolation is a HUGELY useful concept in game dev, and I highly recommend doing further research

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Using the pattern current = Lerp(current, target, weight) gives you what's called an Exponential Moving Average. Just note that these are a little tricky to scale with a variable framerate, since the change over time is non-linear. You can use the formula in this answer to ensure you don't get unexpected changes in the smoothing speed as the framerate varies. \$\endgroup\$
    – DMGregory
    Commented Aug 2, 2017 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .