ProceduralAnimator plays on demand
This commit is contained in:
@@ -43,6 +43,9 @@ public class ProceduralAnimator : MonoBehaviour
|
||||
[Tooltip("Custom animation curve")]
|
||||
public AnimationCurve Curve = AnimationCurve.EaseInOut(0, 0, 1, 1);
|
||||
|
||||
[Tooltip("Play on Awake")]
|
||||
public bool PlayOnAwake = true;
|
||||
|
||||
[Tooltip("Animation speed multiplier")]
|
||||
public float Duration = 4;
|
||||
|
||||
@@ -76,11 +79,20 @@ public class ProceduralAnimator : MonoBehaviour
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private State _state;
|
||||
|
||||
private bool _started;
|
||||
private float _timeOffset;
|
||||
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
_timeOffset = Time.timeSinceLevelLoad;
|
||||
_started = _settings.PlayOnAwake;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
_state.InitialPosition = transform.localPosition;
|
||||
@@ -92,10 +104,10 @@ public class ProceduralAnimator : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_state.IsFinished)
|
||||
if (_state.IsFinished || !_started)
|
||||
return;
|
||||
|
||||
_state.CurrentTime = Time.timeSinceLevelLoad + _settings.TimeOffset * _settings.Duration;
|
||||
_state.CurrentTime = Time.timeSinceLevelLoad + _settings.TimeOffset * _settings.Duration - _timeOffset;
|
||||
|
||||
float normalizedTime = GetNormalizedTime(_state.CurrentTime);
|
||||
float easedTime = ApplyEasing(normalizedTime);
|
||||
@@ -106,6 +118,17 @@ public class ProceduralAnimator : MonoBehaviour
|
||||
ApplyScale(balanceTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Play animation from initial state.
|
||||
/// </summary>
|
||||
[ContextMenu("PlayAnimation")]
|
||||
public void PlayAnimation()
|
||||
{
|
||||
ResetAnimation();
|
||||
_timeOffset = Time.timeSinceLevelLoad;
|
||||
_started = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets animation to initial state.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user