5 using System.Collections.Generic;
24 public enum LerpTypes { Linear, EaseIn, EaseOut, EaseInOut, Free }
26 [Tooltip(
"The object to animate")]
29 [Tooltip(
"The rotation value to animate to")]
32 [Tooltip(
"The type of ease to apply to the tween")]
35 [Tooltip(
"Duration of the animation in seconds")]
36 public float LerpTime = 1f;
38 [Tooltip(
"auto start? or status")]
39 public bool IsRunning =
false;
41 [Tooltip(
"Use the localRotation instead of world rotation")]
42 public bool ToLocalTransform =
false;
44 [Tooltip(
"animation complete!")]
48 private float mLerpTimeCounter;
51 private Quaternion mStartValue;
59 if (TargetObject == null)
61 TargetObject = this.gameObject;
63 mStartValue = GetRotation();
71 if (TargetObject == null)
73 TargetObject = this.gameObject;
76 mStartValue = GetRotation();
88 this.transform.localRotation = mStartValue;
92 this.transform.rotation = mStartValue;
103 TargetValue = mStartValue;
104 mStartValue = TargetValue;
105 mLerpTimeCounter = 0;
118 private Quaternion GetRotation()
120 return ToLocalTransform ? TargetObject.transform.localRotation : TargetObject.transform.rotation;
129 private Quaternion GetNewRotation(Quaternion currentRotation,
float percent)
131 Quaternion newPosition = Quaternion.identity;
135 newPosition = Quaternion.Lerp(mStartValue, TargetValue, percent);
138 newPosition = Quaternion.Lerp(mStartValue, TargetValue, QuadEaseIn(0, 1, percent));
141 newPosition = Quaternion.Lerp(mStartValue, TargetValue, QuadEaseOut(0, 1, percent));
144 newPosition = Quaternion.Lerp(mStartValue, TargetValue, QuadEaseInOut(0, 1, percent));
147 newPosition = Quaternion.Lerp(currentRotation, TargetValue, percent);
159 return e * (v /= 1) * v + s;
164 return -e * (v /= 1) * (v - 2) + s;
170 return e / 2 * v * v + s;
172 return -e / 2 * ((--v) * (v - 2) - 1) + s;
178 private void Update()
181 if (IsRunning && LerpType !=
LerpTypes.Free)
184 mLerpTimeCounter += Time.deltaTime;
185 float percent = mLerpTimeCounter / LerpTime;
188 if (ToLocalTransform)
190 this.transform.localRotation = GetNewRotation(this.transform.localRotation, percent);
194 this.transform.rotation = GetNewRotation(this.transform.rotation, percent);
206 bool wasRunning = IsRunning;
209 if (ToLocalTransform)
211 this.transform.localRotation = GetNewRotation(this.transform.localRotation, LerpTime * Time.deltaTime);
212 IsRunning = this.transform.localRotation != TargetValue;
216 this.transform.rotation = GetNewRotation(this.transform.rotation, LerpTime * Time.deltaTime);
217 IsRunning = this.transform.rotation != TargetValue;
221 if (IsRunning != wasRunning && !IsRunning)