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

32 lines
764 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>
{}
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;
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
{
2019-10-30 10:05:03 +00:00
input_ = value;
2019-10-27 14:46:25 +00:00
}
2019-10-30 10:05:03 +00:00
private void Update()
2019-10-27 14:46:25 +00:00
{
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
}