13 [Tooltip(
"When interpolating, use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
14 public bool UseUnscaledTime =
true;
18 private const float smallNumber = 0.0000001f;
21 public float PositionPerSecond = 30.0f;
24 public float RotationDegreesPerSecond = 720.0f;
27 public float RotationSpeedScaler = 0.0f;
30 public float ScalePerSecond = 5.0f;
35 public bool SmoothLerpToTarget =
false;
37 public float SmoothPositionLerpRatio = 0.5f;
39 public float SmoothRotationLerpRatio = 0.5f;
41 public float SmoothScaleLerpRatio = 0.5f;
44 private Vector3 targetPosition;
49 public bool AnimatingPosition {
get;
private set; }
52 private Quaternion targetRotation;
57 public bool AnimatingRotation {
get;
private set; }
60 private Quaternion targetLocalRotation;
65 public bool AnimatingLocalRotation {
get;
private set; }
68 private Vector3 targetLocalScale;
73 public bool AnimatingLocalScale {
get;
private set; }
88 public Vector3 PositionVelocity {
get;
private set; }
90 private Vector3 oldPosition = Vector3.zero;
99 return (AnimatingPosition || AnimatingRotation || AnimatingLocalRotation || AnimatingLocalScale);
105 targetPosition = transform.position;
106 targetRotation = transform.rotation;
107 targetLocalRotation = transform.localRotation;
108 targetLocalScale = transform.localScale;
120 bool wasRunning = Running;
122 targetPosition = target;
124 float magsq = (targetPosition - transform.position).sqrMagnitude;
125 if (magsq > smallNumber)
127 AnimatingPosition =
true;
130 if (InterpolationStarted != null && !wasRunning)
132 InterpolationStarted();
138 transform.position = target;
139 AnimatingPosition =
false;
150 bool wasRunning = Running;
152 targetRotation = target;
154 if (Quaternion.Dot(transform.rotation, target) < 1.0f)
156 AnimatingRotation =
true;
159 if (InterpolationStarted != null && !wasRunning)
161 InterpolationStarted();
167 transform.rotation = target;
168 AnimatingRotation =
false;
179 bool wasRunning = Running;
181 targetLocalRotation = target;
183 if (Quaternion.Dot(transform.localRotation, target) < 1.0f)
185 AnimatingLocalRotation =
true;
188 if (InterpolationStarted != null && !wasRunning)
190 InterpolationStarted();
196 transform.localRotation = target;
197 AnimatingLocalRotation =
false;
208 bool wasRunning = Running;
210 targetLocalScale = target;
212 float magsq = (targetLocalScale - transform.localScale).sqrMagnitude;
213 if (magsq > Mathf.Epsilon)
215 AnimatingLocalScale =
true;
218 if (InterpolationStarted != null && !wasRunning)
220 InterpolationStarted();
226 transform.localScale = target;
227 AnimatingLocalScale =
false;
247 Vector3 distance = (target - start);
250 if (distance.sqrMagnitude <= Mathf.Epsilon)
256 Vector3 deltaMove = distance * Mathf.Clamp(deltaTime * speed, 0.0f, 1.0f);
258 return start + deltaMove;
263 float deltaTime = UseUnscaledTime
264 ? Time.unscaledDeltaTime
267 bool interpOccuredThisFrame =
false;
269 if (AnimatingPosition)
271 Vector3 lerpTargetPosition = targetPosition;
272 if (SmoothLerpToTarget)
274 lerpTargetPosition = Vector3.Lerp(transform.position, lerpTargetPosition, SmoothPositionLerpRatio);
277 Vector3 newPosition = NonLinearInterpolateTo(transform.position, lerpTargetPosition, deltaTime, PositionPerSecond);
278 if ((targetPosition - newPosition).sqrMagnitude <= smallNumber)
281 newPosition = targetPosition;
282 AnimatingPosition =
false;
286 interpOccuredThisFrame =
true;
289 transform.position = newPosition;
292 PositionVelocity = oldPosition - newPosition;
293 oldPosition = newPosition;
297 if (AnimatingRotation)
299 Quaternion lerpTargetRotation = targetRotation;
300 if (SmoothLerpToTarget)
302 lerpTargetRotation = Quaternion.Lerp(transform.rotation, lerpTargetRotation, SmoothRotationLerpRatio);
305 float angleDiff = Quaternion.Angle(transform.rotation, lerpTargetRotation);
306 float speedScale = 1.0f + (Mathf.Pow(angleDiff, RotationSpeedScaler) / 180.0f);
307 float ratio = Mathf.Clamp01((speedScale * RotationDegreesPerSecond * deltaTime) / angleDiff);
309 if (angleDiff < Mathf.Epsilon)
311 AnimatingRotation =
false;
312 transform.rotation = targetRotation;
317 transform.rotation = Quaternion.Slerp(transform.rotation, lerpTargetRotation, ratio);
318 interpOccuredThisFrame =
true;
323 if (AnimatingLocalRotation)
325 Quaternion lerpTargetLocalRotation = targetLocalRotation;
326 if (SmoothLerpToTarget)
328 lerpTargetLocalRotation = Quaternion.Lerp(transform.localRotation, lerpTargetLocalRotation, SmoothRotationLerpRatio);
331 float angleDiff = Quaternion.Angle(transform.localRotation, lerpTargetLocalRotation);
332 float speedScale = 1.0f + (Mathf.Pow(angleDiff, RotationSpeedScaler) / 180.0f);
333 float ratio = Mathf.Clamp01((speedScale * RotationDegreesPerSecond * deltaTime) / angleDiff);
335 if (angleDiff < Mathf.Epsilon)
337 AnimatingLocalRotation =
false;
338 transform.localRotation = targetLocalRotation;
343 transform.localRotation = Quaternion.Slerp(transform.localRotation, lerpTargetLocalRotation, ratio);
344 interpOccuredThisFrame =
true;
348 if (AnimatingLocalScale)
350 Vector3 lerpTargetLocalScale = targetLocalScale;
351 if (SmoothLerpToTarget)
353 lerpTargetLocalScale = Vector3.Lerp(transform.localScale, lerpTargetLocalScale, SmoothScaleLerpRatio);
356 Vector3 newScale = NonLinearInterpolateTo(transform.localScale, lerpTargetLocalScale, deltaTime, ScalePerSecond);
357 if ((targetLocalScale - newScale).sqrMagnitude <= smallNumber)
360 newScale = targetLocalScale;
361 AnimatingLocalScale =
false;
365 interpOccuredThisFrame =
true;
368 transform.localScale = newScale;
372 if (!interpOccuredThisFrame)
374 if (InterpolationDone != null)
389 transform.position = TargetPosition;
390 transform.rotation = TargetRotation;
391 transform.localRotation = TargetLocalRotation;
392 transform.localScale = TargetLocalScale;
394 AnimatingPosition =
false;
395 AnimatingLocalScale =
false;
396 AnimatingRotation =
false;
397 AnimatingLocalRotation =
false;
401 if (InterpolationDone != null)
417 if (InterpolationDone != null)
429 targetPosition = transform.position;
430 targetRotation = transform.rotation;
431 targetLocalRotation = transform.localRotation;
432 targetLocalScale = transform.localScale;
434 AnimatingPosition =
false;
435 AnimatingRotation =
false;
436 AnimatingLocalRotation =
false;
437 AnimatingLocalScale =
false;
447 public Vector3 TargetPosition
451 if (AnimatingPosition)
453 return targetPosition;
455 return transform.position;
464 public Quaternion TargetRotation
468 if (AnimatingRotation)
470 return targetRotation;
472 return transform.rotation;
481 public Quaternion TargetLocalRotation
485 if (AnimatingLocalRotation)
487 return targetLocalRotation;
489 return transform.localRotation;
498 public Vector3 TargetLocalScale
502 if (AnimatingLocalScale)
504 return targetLocalScale;
506 return transform.localScale;