# 4. Blinks Gaze and Idle Life Part of [[Facial Animation and Lip Sync]]. Previous: [[3. Expressions from Ink Mood Tags]]. Next: [[5. Lip Sync from Audio Amplitude]]. A face that never blinks and stares straight ahead looks dead even with perfect lip sync. Blinks and eye movement are the cheapest life you can add: one blendshape pair and two bone rotations. ## 4.1 What a blink looks like These are rules of thumb used in animation, not measurements from this series: - People blink every few seconds, irregularly. A metronome blink is the first thing players notice. This series defaults to a uniform random **2–6 s** interval. - The lid **closes fast and opens slower**. The defaults are 60 ms close, 30 ms hold, and 120 ms open with an ease-out. - Sometimes the blink comes **double** (15% by default). - People blink when their **gaze jumps** and at emotional beats, so expose `BlinkNow()`. ## 4.2 BlinkScheduler (pure C#) It's seeded, so tests are deterministic and two NPCs with different seeds don't blink in unison. It returns lid closure 0..1 for an absolute time, so it works with any clock (scaled, unscaled, or a Timeline's time). ```csharp using System; /// <summary> /// Procedural blink timing: random intervals, occasional double blinks, and an asymmetric /// close/hold/open curve (eyes close faster than they open). Seeded, so tests are deterministic. /// </summary> public sealed class BlinkScheduler { readonly Random _rng; double _nextBlinkAt; double _blinkStart = double.NegativeInfinity; bool _queuedDouble; public BlinkScheduler(int seed, float minInterval = 2f, float maxInterval = 6f, float doubleBlinkChance = 0.15f) { _rng = new Random(seed); MinInterval = minInterval; MaxInterval = maxInterval; DoubleBlinkChance = doubleBlinkChance; _nextBlinkAt = NextInterval(); } public float MinInterval { get; set; } public float MaxInterval { get; set; } public float DoubleBlinkChance { get; set; } public float CloseSeconds { get; set; } = 0.06f; public float HoldSeconds { get; set; } = 0.03f; public float OpenSeconds { get; set; } = 0.12f; public float Duration => CloseSeconds + HoldSeconds + OpenSeconds; /// <summary>Force a blink now (on a gaze shift, a surprise, or a cut).</summary> public void BlinkNow(double now) { _blinkStart = now; _nextBlinkAt = now + Duration + NextInterval(); } /// <summary>Returns eyelid closure 0..1 for time `now` (seconds). Call every frame with increasing time.</summary> public float Evaluate(double now) { if (now >= _nextBlinkAt && now - _blinkStart >= Duration) { _blinkStart = now; if (_queuedDouble) { _queuedDouble = false; _nextBlinkAt = now + Duration + NextInterval(); } else if (_rng.NextDouble() < DoubleBlinkChance) { _queuedDouble = true; _nextBlinkAt = now + Duration + 0.08; // second blink right after the first } else { _nextBlinkAt = now + Duration + NextInterval(); } } return Curve(now - _blinkStart); } public float Curve(double t) { if (t < 0 || t >= Duration) return 0f; if (t < CloseSeconds) return (float)(t / CloseSeconds); if (t < CloseSeconds + HoldSeconds) return 1f; var u = (t - CloseSeconds - HoldSeconds) / OpenSeconds; return (float)(1.0 - u * u); // ease-out reopen } double NextInterval() => MinInterval + _rng.NextDouble() * Math.Max(0, MaxInterval - MinInterval); } ``` ## 4.3 BlinkDriver It writes both lids to the blink layer (Override), so blinks win over a "surprised" `eyeWide`. The seed comes from `GetEntityId()`. In 6000.5, `GetInstanceID()` is a compile error (CS0619), and `EntityId` replaces it. ```csharp using UnityEngine; /// <summary>Drives both eyelids from a BlinkScheduler; anything can call Blink() (gaze shifts, cuts).</summary> [RequireComponent(typeof(FaceRig))] public sealed class BlinkDriver : MonoBehaviour { [SerializeField] string leftChannel = "eyeBlinkLeft"; [SerializeField] string rightChannel = "eyeBlinkRight"; [SerializeField] Vector2 intervalRange = new Vector2(2f, 6f); [SerializeField, Range(0f, 1f)] float doubleBlinkChance = 0.15f; FaceRig _rig; BlinkScheduler _scheduler; int _left, _right; void Start() { _rig = GetComponent<FaceRig>(); _left = _rig.Channel(leftChannel); _right = _rig.Channel(rightChannel); // Seed from the entity id so two identical NPCs don't blink in sync. _scheduler = new BlinkScheduler(gameObject.GetEntityId().GetHashCode(), intervalRange.x, intervalRange.y, doubleBlinkChance); } public void Blink() => _scheduler?.BlinkNow(Time.timeAsDouble); void Update() { var closed = _scheduler.Evaluate(Time.timeAsDouble) * 100f; _rig.Blink.Set(_left, closed); _rig.Blink.Set(_right, closed); } } ``` > [!tip] Blinks in cutscenes > Call `Blink()` on a camera cut. Film editors often relate cuts to blinks (Walter Murch's *In the Blink of an Eye*), and a blink just after a cut makes the new shot feel less abrupt. Hook it to a Timeline signal ([[Timeline Advanced]]). ## 4.4 EyeGaze Eyes are usually **bones** (`LeftEye`/`RightEye` on a Humanoid). `EyeGaze`: - converts the target into **head space** and computes yaw and pitch - **clamps** to ±30° yaw and ±20° pitch. Beyond 1.5× the yaw limit, the eyes return to centre, because a real person would turn their head (pair this with head IK from [[Procedural Animation and IK]]). - adds **micro-saccades**: tiny random jumps every 0.4–1.6 s. Eyes are never perfectly still. - **smooths** with an exponential follow (60 ms), fast like real eyes - **blinks** when the gaze jumps more than 20° ```csharp using UnityEngine; /// <summary> /// Points eye bones at a target with clamped yaw/pitch and small saccades, and blinks on big gaze shifts. /// Runs in LateUpdate after the Animator. Eye bones must face +Z in their rest pose (or set restForward). /// </summary> [DefaultExecutionOrder(90)] public sealed class EyeGaze : MonoBehaviour { [SerializeField] Transform leftEye; [SerializeField] Transform rightEye; [SerializeField] Transform head; [SerializeField] Transform target; [SerializeField] float maxYaw = 30f; [SerializeField] float maxPitch = 20f; [SerializeField, Min(0.01f)] float followSeconds = 0.06f; [SerializeField] float saccadeDegrees = 1.5f; [SerializeField] Vector2 saccadeInterval = new Vector2(0.4f, 1.6f); [SerializeField] float blinkOnShiftDegrees = 20f; [SerializeField] BlinkDriver blink; Quaternion _leftRest, _rightRest; Vector2 _current, _saccade; float _nextSaccade; public Transform Target { get => target; set => target = value; } void Start() { _leftRest = leftEye.localRotation; _rightRest = rightEye.localRotation; } void LateUpdate() { var desired = Vector2.zero; if (target != null) { var local = head.InverseTransformPoint(target.position); var yaw = Mathf.Atan2(local.x, local.z) * Mathf.Rad2Deg; var pitch = -Mathf.Atan2(local.y, new Vector2(local.x, local.z).magnitude) * Mathf.Rad2Deg; // Beyond the eye range, eyes rest at centre (the head or body should turn instead). if (Mathf.Abs(yaw) <= maxYaw * 1.5f) desired = new Vector2(Mathf.Clamp(yaw, -maxYaw, maxYaw), Mathf.Clamp(pitch, -maxPitch, maxPitch)); } if (Time.time >= _nextSaccade) { _saccade = Random.insideUnitCircle * saccadeDegrees; _nextSaccade = Time.time + Random.Range(saccadeInterval.x, saccadeInterval.y); } desired += _saccade; if (blink != null && (desired - _current).magnitude > blinkOnShiftDegrees) blink.Blink(); var k = 1f - Mathf.Exp(-Time.deltaTime / followSeconds); _current = Vector2.Lerp(_current, desired, k); var look = Quaternion.Euler(_current.y, _current.x, 0f); leftEye.localRotation = _leftRest * look; rightEye.localRotation = _rightRest * look; } } ``` Setup notes: - The rest pose must look forward. If your eye bones' forward isn't +Z, rotate a child "eye aim" transform instead of the bone, or bake the offset into `_leftRest`. - `head` should be the head bone, so gaze is relative to where the head points after animation. - It runs at `[DefaultExecutionOrder(90)]` in LateUpdate, after the Animator and before `FaceRig`. If the Humanoid has **Eyes** mapped and an animation drives them, `EyeGaze` overwrites them each frame, which is what you want. > [!note] Not verified > Sign conventions (pitch up vs down, yaw left vs right) depend on bone orientation in your rig. If the eyes look the wrong way, negate that axis. Nothing here was checked on a real rig. ## 4.5 Design lens Where NPCs look is level and narrative design. A listener who looks at the speaker teaches the player who is talking. An NPC who looks at a door teaches the player where to go. [[Level Up! (Review)]] keeps returning to leading the player's eye with the world itself, and NPC gaze is one of the cheapest tools for that. `InkFaceDirector` does it per line, and you can do it per beat (look at the thing being described). ## Exercises 1. Make blink rate depend on mood: more blinks when `nervous`, fewer when `angry` (a stare). Hint: add a per-mood interval range to `ExpressionLibrary`. 2. Add `lookAway`: every 4–10 s while *listening*, look 15° off the speaker for 0.5–1.5 s. Constant eye contact reads as hostile.