# 2. A Hand-Rolled Tween Runner Part of [[Tweening and UI Juice]]. Back: [[1. Juice TimeScale and When to Tween]]. Next: [[3. UI Toolkit USS Transitions]]. > [!warning] Code not compiled in Unity > `TweenRunner` and `WorldJuice` were type-checked against Unity 6000.5.8f1 (`Time.deltaTime`, `Time.unscaledDeltaTime`, `AnimationCurve.Evaluate`, `AnimationCurve.EaseInOut`). They have not been run in Play Mode. ## 2.1 One runner, many short tweens You do not need a package to lerp a float over a curve. `TweenRunner` is an Update loop (the **Update Method** in [[Game Programming Patterns (Review)]]) that owns a small list of slots, advances each with official scaled or unscaled delta time, and evaluates an `AnimationCurve`. Duration 0 snaps to the end and fires the completion callback immediately. `Play` returns a slot id so you can `Stop` a punch when a second punch starts. `StopAll` is what a screen teardown should call. The `onStep` / `onComplete` delegates will allocate if you pass a fresh lambda every time. For a menu that opens once, that is fine. For a machine gun of hit-punches, reuse a method group or cache the delegate. ```csharp using System; using System.Collections.Generic; using UnityEngine; /// <summary> /// A small Update-driven tween runner. Uses official Time.deltaTime or /// Time.unscaledDeltaTime, and AnimationCurve.Evaluate for easing. /// No third-party package. /// </summary> public sealed class TweenRunner : MonoBehaviour { struct Slot { public float start; public float end; public float duration; public float elapsed; public bool unscaled; public AnimationCurve curve; public Action<float> onStep; public Action onComplete; public bool active; } readonly List<Slot> _slots = new List<Slot>(8); public int Play(float start, float end, float duration, AnimationCurve curve, bool unscaled, Action<float> onStep, Action onComplete = null) { if (duration <= 0f) { onStep?.Invoke(end); onComplete?.Invoke(); return -1; } var slot = new Slot { start = start, end = end, duration = duration, elapsed = 0f, unscaled = unscaled, curve = curve, onStep = onStep, onComplete = onComplete, active = true }; for (var i = 0; i < _slots.Count; i++) { if (_slots[i].active) continue; _slots[i] = slot; onStep?.Invoke(start); return i; } _slots.Add(slot); onStep?.Invoke(start); return _slots.Count - 1; } public void Stop(int id, bool complete) { if (id < 0 || id >= _slots.Count || !_slots[id].active) return; var slot = _slots[id]; slot.active = false; _slots[id] = slot; if (complete) { slot.onStep?.Invoke(slot.end); slot.onComplete?.Invoke(); } } public void StopAll(bool complete) { for (var i = 0; i < _slots.Count; i++) { if (_slots[i].active) Stop(i, complete); } } void Update() { var scaled = Time.deltaTime; var unscaled = Time.unscaledDeltaTime; for (var i = 0; i < _slots.Count; i++) { var slot = _slots[i]; if (!slot.active) continue; slot.elapsed += slot.unscaled ? unscaled : scaled; var t = Mathf.Clamp01(slot.elapsed / slot.duration); var eased = slot.curve != null ? slot.curve.Evaluate(t) : t; slot.onStep?.Invoke(Mathf.LerpUnclamped(slot.start, slot.end, eased)); if (t >= 1f) { slot.active = false; _slots[i] = slot; slot.onComplete?.Invoke(); } else { _slots[i] = slot; } } } } ``` A default ease if you do not author a curve in the Inspector: `AnimationCurve.EaseInOut(0f, 0f, 1f, 1f)` (official 6000.5: "creates an ease-in and out curve starting at timeStart, valueStart and ending at timeEnd, valueEnd"). `Evaluate` at `t` in 0..1 is the eased weight you pass to `Mathf.LerpUnclamped`. ## 2.2 A world punch ```csharp using UnityEngine; /// <summary> /// Example world-space juice: punch a transform's local scale with TweenRunner. /// Use unscaled: false so gameplay hits follow Time.timeScale. /// </summary> public sealed class WorldJuice : MonoBehaviour { [SerializeField] TweenRunner runner; [SerializeField] AnimationCurve punch = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f); [SerializeField] float punchDuration = 0.18f; [SerializeField] float punchScale = 1.15f; Vector3 _rest = Vector3.one; int _id = -1; void Awake() { _rest = transform.localScale; if (runner == null) runner = GetComponent<TweenRunner>(); } public void Punch() { if (runner == null) return; runner.Stop(_id, complete: true); _id = runner.Play(0f, 1f, punchDuration, punch, unscaled: false, onStep: t => { var s = Mathf.Lerp(punchScale, 1f, t); transform.localScale = _rest * s; }, onComplete: () => transform.localScale = _rest); } } ``` Hook `Punch()` from a damage event or a pickup. Because `unscaled: false`, the punch freezes with a pause — correct for a gameplay hit. For a pause-menu icon, call `runner.Play(..., unscaled: true, ...)`. You can tween opacity, a slider value, or a world position the same way: the runner only produces a float. Convert it in `onStep`. Next: [[3. UI Toolkit USS Transitions]].