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-11-22 17:38:28 +00:00
|
|
|
|
{ }
|
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;
|
2019-11-22 17:38:28 +00:00
|
|
|
|
[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
|
|
|
|
{
|
2019-11-22 17:38:28 +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-11-22 17:38:28 +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
|
|
|
|
}
|