问题
错误(帧率依赖)
// This runs every frame with a fixed 10% step. // At 30fps: 30 steps/sec. At 144fps: 144 steps/sec. // Players on faster machines see faster smoothing. value = Mathf.Lerp(value, target, 0.1f);
修复(帧率无关)
// Same behavior at any frame rate. // Uses exp(-speed * dt) which naturally // compensates for variable frame times. value = Smooth.Exp(value, target, 10f, Time.deltaTime);
快速入门
using DawgTools.Smooth; // --- Exponential smoothing --- // Direct replacement for broken Lerp transform.position = Smooth.Exp(transform.position, target, 10f, Time.deltaTime); // Designer-friendly: "halfway to target in 0.2 seconds" value = Smooth.ExpHalfLife(value, target, 0.2f, Time.deltaTime); // Angle-aware (handles 0/360 wrap correctly) angle = Smooth.ExpAngle(angle, targetAngle, 8f, Time.deltaTime); // --- 弹簧阻尼器 --- // Critically damped: fastest convergence, no overshoot pos = Spring.Critical(pos, target, ref velocity, 5f, Time.deltaTime); // Bouncy: overshoots and oscillates scale = Spring.Bouncy(scale, targetScale, ref scaleVel, 8f, 0.5f, Time.deltaTime); // --- Migration --- // Convert your existing broken lerp value to correct speed float speed = Smooth.SpeedFromBrokenLerpT(0.1f, 60f); // ~6.32 // --- Easing --- float t = Ease.InOutCubic(progress); value = Ease.Lerp(start, end, progress, EaseType.InOutElastic);
指数平滑 - Smooth
静态类。所有方法都重载了float、Vector2、Vector3、Quaternion和Color。
| Method | Description |
|---|---|
Smooth.Exp(value, target, speed, dt) | 使用exp(-speed * dt)的指数平滑。核心修复。 |
Smooth.ExpHalfLife(value, target, halfLife, dt) | 相同数学,以半衰期(秒)参数化。设计师友好。 |
Smooth.ExpAngle(value, target, speed, dt) | 角度感知版本。正确处理0/360回绕。 |
Smooth.SpeedFromBrokenLerpT(t, fps) | 将现有错误的lerp t值转换为参考帧率下的正确速度。 |
Smooth.ConvergenceTime(speed, pct) | 到达指定百分比(默认99%)收敛所需时间。 |
弹簧阻尼器 - Spring
具有自适应子步进帧率稳定性的二阶弹簧动力学。支持float、Vector2、Vector3重载。
| Method | Description |
|---|---|
Spring.Critical(val, target, ref vel, freq, dt) | 临界阻尼。无过冲的最快收敛。 |
Spring.Bouncy(val, target, ref vel, freq, bounciness, dt) | 可配置弹性度 (0-1) 的弹性/弹簧感。 |
Spring.Damped(val, target, ref vel, freq, damping, dt) | 任意阻尼比的通用二阶弹簧。 |
缓动函数 - Ease
30+标准曲线。每种都有In、Out和InOut变体。
| 族 | 曲线 |
|---|---|
| Polynomial | Quad, Cubic, Quart, Quint |
| Trigonometric | Sine |
| Exponential | Expo, Circ |
| Overshoot | Back, Elastic, Bounce |
| Hermite | SmoothStep, SmootherStep (Perlin variant) |
Ease.Lerp(start, end, t, EaseType) 在一次调用中结合缓动+插值。
Editor tools
Window > DAWG Tools > Smooth Inspector
- 指数模式 - 在30/60/144fps下naive Lerp vs Smooth.Exp的并排图表。直观查看帧率依赖性。
- 弹簧模式 - 响应曲线可视化,显示弹性设置的过冲、稳定时间和振铃。
- 缓动模式 - 30+缓动函数的曲线形状预览。
- dt压力测试 - 在恒定FPS、抖动帧、FixedUpdate和自定义tick速率下测试平滑。
- 迁移助手 - 将现有错误的lerp值转换为正确的指数速度。可直接复制粘贴的代码。
安装
// Unity Package Manager > Add package from disk // Navigate to package.json and select it // Then import the demo scene: // Package Manager > Motion Smoothing Pro > Samples > Import
Requires Unity 2021.3 LTS or newer. 无外部依赖.