Created
January 23, 2019 10:44
-
-
Save HyroVitalyProtago/56f7e8b478db658e3489dacf8b8f17a7 to your computer and use it in GitHub Desktop.
LowPassFilter for unity in c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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