Tracking speed at which a float changes value?

I want something to happen if a float changes value too quickly. The player is controlling the speed at which this float changes, i.e. if the “height” float variable decreases really quickly something happens, but if the height variable decreases very slowly and gradually something else happens.

I’m eventually gonna connect this float to how intensely an Arduino sensor changes values, but for now it’s just how quickly a keyboard press is happening.

I know this is about comparing the current value with a past value, and Time.deltaTime, but other than that I’m stuck. Any advice is appreciated!

It’s exactly that. Subtract the previous reading from the current, then divide it by the Time.deltaTime, but make sure you ONLY do that when Time is moving forward with enough speed to make stable results, given the accuracy of floating point.

Something like:

private float previous, current;

void Update()
{
  // TODO: set current to new value here.

  float deltaTime = Time.deltaTime;
  if (deltaTime > 0.001f)
  {
    float deltaValue = current - previous;
    float changeRate = deltaValue / deltaTime;

    // TODO: use changeRate for your decision here
  }
  // age data
  previous = current;
}

A common improvement to this is to use a low pass filter (Mathf.Lerp() can help you make a cheese one) to require more than a single frame out of range to trigger it.

Lerping to smooth things out: