diff options
| author | chai <215380520@qq.com> | 2023-11-02 11:51:31 +0800 |
|---|---|---|
| committer | chai <215380520@qq.com> | 2023-11-02 11:51:31 +0800 |
| commit | 7f493f682503f5186308de7b8f74b5b49233cfe4 (patch) | |
| tree | 8a91e2056bc79788ee4735dce88b8d516ba12beb /GameCode/PositionAnimation.cs | |
+init
Diffstat (limited to 'GameCode/PositionAnimation.cs')
| -rw-r--r-- | GameCode/PositionAnimation.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/GameCode/PositionAnimation.cs b/GameCode/PositionAnimation.cs new file mode 100644 index 0000000..c50408d --- /dev/null +++ b/GameCode/PositionAnimation.cs @@ -0,0 +1,42 @@ +using System.Collections; +using UnityEngine; + +public class PositionAnimation : OneShotAnimationBase +{ + public AnimationCurve curve; + + public float duration = 0.75f; + + public Vector3 targetPosition; + + public Transform transformToAnimate; + + private Vector3 initialPosition; + + private void Start() + { + initialPosition = transformToAnimate.localPosition; + } + + private IEnumerator Animate() + { + transformToAnimate.localPosition = initialPosition; + float timer = 0f; + while (timer < duration) + { + transformToAnimate.localPosition = Vector3.Lerp(initialPosition, targetPosition, curve.Evaluate(timer / duration)); + timer += Time.deltaTime; + yield return null; + } + transformToAnimate.localPosition = Vector3.Lerp(initialPosition, targetPosition, curve.Evaluate(1f)); + } + + public override void Trigger() + { + if (base.gameObject.activeInHierarchy) + { + StopAllCoroutines(); + StartCoroutine(Animate()); + } + } +} |
