public static IEnumerator TweenRoutine(this Animator animator, Vector3? position, Quaternion? rotation,
float startTimeNormalized, float endTimeNormalized, Action onComplete = null)
{
if (startTimeNormalized < animator.GetCurrentAnimatorStateInfo(0).normalizedTime)
{
startTimeNormalized = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
}
while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < startTimeNormalized)
{
yield return null;
}
var transform = animator.transform;
var startPosition = transform.position;
var startRotation = transform.rotation;
while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < endTimeNormalized)
{
var percentage = (animator.GetCurrentAnimatorStateInfo(0).normalizedTime - startTimeNormalized) /
(endTimeNormalized - startTimeNormalized);
percentage = Mathf.Clamp(percentage, 0, 1);
if (position != null)
{
transform.position = Vector3.Lerp(startPosition, (Vector3) position, percentage);
}
if (rotation != null)
{
transform.rotation = Quaternion.Lerp(startRotation, (Quaternion) rotation, percentage);
}
yield return null;
}
if (onComplete != null)
{
onComplete();
}
}