soundvision/UnityProject/Assets/Scripts/Math/Smoother.cs

36 lines
895 B
C#
Raw Normal View History

2019-10-27 14:46:25 +00:00
using System;
2019-10-30 10:05:03 +00:00
using System.Timers;
2019-10-27 14:46:25 +00:00
using UnityEngine;
using UnityEngine.Events;
[Serializable]
public class UnitySmoothEvent : UnityEvent<float>
{ }
2019-10-27 14:46:25 +00:00
public class Smoother : MonoBehaviour
{
2019-10-30 10:05:03 +00:00
[SerializeField, Range(1f, 10f)] private float attackSmooth = 1f;
[SerializeField, Range(1f, 10f)] private float releaseSmooth = 1f;
[SerializeField] private bool ignore0;
2019-10-27 14:46:25 +00:00
2019-10-30 10:05:03 +00:00
[SerializeField] private UnitySmoothEvent onSmoothProcessed;
2019-10-27 14:46:25 +00:00
2019-10-30 10:05:03 +00:00
private float input_;
private float previous_;
2019-10-27 14:46:25 +00:00
2019-10-30 10:05:03 +00:00
public void OnValueChanged(float value)
2019-10-27 14:46:25 +00:00
{
if (!(ignore0 && value == 0.0f))
{
input_ = value; //Set new input Value
}
2019-10-27 14:46:25 +00:00
}
2019-10-30 10:05:03 +00:00
private void Update()
{
2019-10-30 10:05:03 +00:00
var distance = input_ - previous_;
previous_ += distance > 0f? (1f/attackSmooth) * distance : (1f/releaseSmooth) * distance;
onSmoothProcessed.Invoke(previous_);
2019-10-27 14:46:25 +00:00
}
2019-10-30 10:05:03 +00:00
}