2 using System.Collections;
15 [Tooltip(
"The smoothing factor."), Range(0, 1)]
16 public float Epsilon = 0.5f;
18 [Tooltip(
"The Precision."), Range(0, 0.1f)]
19 public float Precision = 0.05f;
23 get {
return target; }
34 StartCoroutine(MoveTo(target));
38 [Tooltip(
"The mode. If set to 'Horizontal', will leave the y component unchanged. Full means the object will move in all 3D coordinates.")]
39 public Mode SmoothMoveMode = Mode.Full;
41 private Vector3 target;
42 private Action onTargetReached;
43 private IEnumerator co;
45 public void Move(Vector3 to, Action callback =
null)
47 onTargetReached = callback;
52 private IEnumerator MoveTo(Vector3 pTarget)
54 if (SmoothMoveMode == Mode.Horizontal)
58 Vector2 horizontalPosition = MathUtils.HorizontalVector(transform.position);
59 Vector2 horizontalTarget = MathUtils.HorizontalVector(pTarget);
61 while (Vector2.Distance(horizontalPosition, horizontalTarget) > Precision)
63 float t = 1.0f - Mathf.Pow(Epsilon, Time.deltaTime);
64 horizontalPosition = Vector3.Lerp(horizontalPosition, horizontalTarget, t);
66 transform.position = MathUtils.HorizontalVectorToVector3(horizontalPosition, transform.position.y);
71 transform.position = MathUtils.HorizontalVectorToVector3(horizontalTarget, transform.position.y);
73 onTargetReached?.Invoke();
74 onTargetReached =
null;
78 while (Vector3.Distance(transform.position, pTarget) > Precision)
80 float t = 1.0f - Mathf.Pow(Epsilon, Time.deltaTime);
81 transform.position = Vector3.Lerp(transform.position, pTarget, t);
86 transform.position = pTarget;
88 onTargetReached?.Invoke();
89 onTargetReached =
null;
93 public static SmoothMove AddSmoothMove(GameObject go,
float epsilon)
95 var smoothMove = go.AddComponent<
SmoothMove>();
96 smoothMove.Epsilon = epsilon;