Skip to content

Instantly share code, notes, and snippets.

@HyroVitalyProtago
Created January 23, 2019 10:44
Show Gist options
  • Save HyroVitalyProtago/56f7e8b478db658e3489dacf8b8f17a7 to your computer and use it in GitHub Desktop.
Save HyroVitalyProtago/56f7e8b478db658e3489dacf8b8f17a7 to your computer and use it in GitHub Desktop.
LowPassFilter for unity in c#
using UnityEngine;
/// <summary>
/// TODO generic-like?
/// -Warning- use Time.deltaTime
/// </summary>
public class LowPassFilter {
readonly float _rc;
float _lastValue;
public float Value { get; private set; }
public float Next(float value) {
Value = _lastValue + A() * (value - _lastValue);
_lastValue = Value;
return Value;
}
float A() { return Time.unscaledDeltaTime / (_rc + Time.unscaledDeltaTime); }
public LowPassFilter(float rc) { _rc = rc; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment