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