14 [RequireComponent(typeof(SolverHandler))]
15 public abstract class Solver : MonoBehaviour
17 #region public members 18 [Tooltip(
"If true, the position and orientation will be calculated, but not applied, for other components to use")]
19 public bool UpdateLinkedTransform =
false;
21 [Tooltip(
"Position lerp multiplier")]
22 public float MoveLerpTime = 0.1f;
23 [Tooltip(
"Rotation lerp multiplier")]
24 public float RotateLerpTime = 0.1f;
25 [Tooltip(
"Scale lerp multiplier")]
26 public float ScaleLerpTime = 0;
28 [Tooltip(
"If true, the Solver will respect the object's original scale values")]
29 public bool MaintainScale =
true;
41 [Tooltip(
"Working output is smoothed if true. Otherwise snapped")]
42 public bool Smoothing =
true;
44 [Tooltip(
"If > 0, this solver will deactivate after this much time, even if the state is still active")]
45 public float Lifetime = 0;
48 #region private members 50 private float lifetime;
55 solverHandler = GetComponent<SolverHandler>();
57 if (UpdateLinkedTransform && solverHandler == null)
59 Debug.LogError(
"No SolverHandler component found on " + name +
" when UpdateLinkedTransform was set to true! Disabling UpdateLinkedTransform");
60 UpdateLinkedTransform =
false;
63 GoalScale = MaintainScale ==
true ? this.transform.localScale : Vector3.one;
74 if (solverHandler != null)
84 public abstract void SolverUpdate();
92 if (Lifetime > 0 && lifetime >= Lifetime)
106 public virtual void SnapTo(Vector3 position, Quaternion rotation)
108 SnapGoalTo(position, rotation);
110 WorkingPos = position;
111 WorkingRot = rotation;
119 public virtual void SnapGoalTo(Vector3 position, Quaternion rotation)
121 GoalPosition = position;
122 GoalRotation = rotation;
127 GoalPosition += offset;
135 public Vector3 WorkingPos
139 if (UpdateLinkedTransform)
145 return transform.position;
151 if (UpdateLinkedTransform)
157 this.transform.position = value;
165 public Quaternion WorkingRot
169 if (UpdateLinkedTransform)
175 return transform.rotation;
181 if (UpdateLinkedTransform)
187 this.transform.rotation = value;
195 public Vector3 WorkingScale
199 if (UpdateLinkedTransform)
205 return transform.localScale;
211 if (UpdateLinkedTransform)
217 this.transform.localScale = value;
230 public static Vector3
SmoothTo(Vector3 source, Vector3 goal,
float deltaTime,
float lerpTime)
232 return Vector3.Lerp(source, goal, lerpTime == 0 ? 1f : deltaTime / lerpTime);
243 public static Quaternion
SmoothTo(Quaternion source, Quaternion goal,
float deltaTime,
float lerpTime)
245 return Quaternion.Slerp(source, goal, lerpTime == 0 ? 1f : deltaTime / lerpTime);
255 Vector3 pos = this.transform.position;
256 Quaternion rot = this.transform.rotation;
257 Vector3 scale = this.transform.localScale;
260 pos = SmoothTo(pos, GoalPosition, dt, MoveLerpTime);
261 rot = SmoothTo(rot, GoalRotation, dt, RotateLerpTime);
262 scale = SmoothTo(scale, GoalScale, dt, ScaleLerpTime);
264 this.transform.position = pos;
265 this.transform.rotation = rot;
266 this.transform.localScale = scale;
270 this.transform.position = GoalPosition;
271 this.transform.rotation = GoalRotation;
272 this.transform.localScale = GoalScale;
284 WorkingPos = SmoothTo(WorkingPos, GoalPosition, dt, MoveLerpTime);
285 WorkingRot = SmoothTo(WorkingRot, GoalRotation, dt, RotateLerpTime);
286 WorkingScale = SmoothTo(WorkingScale, GoalScale, dt, ScaleLerpTime);
290 WorkingPos = GoalPosition;
291 WorkingRot = GoalRotation;
292 WorkingScale = GoalScale;
303 WorkingPos = SmoothTo(WorkingPos, GoalPosition, solverHandler.
DeltaTime, MoveLerpTime);
307 WorkingPos = GoalPosition;
318 WorkingRot = SmoothTo(WorkingRot, GoalRotation, solverHandler.
DeltaTime, RotateLerpTime);
322 WorkingRot = GoalRotation;
333 WorkingScale = SmoothTo(WorkingScale, GoalScale, solverHandler.
DeltaTime, ScaleLerpTime);
337 WorkingScale = GoalScale;