31 [Header(
"Solver Parameters")]
83 throw new UnityException(
"The chain value is longer than the ancestor chain!");
90 Target =
new GameObject(gameObject.name +
" Target").transform;
91 SetPositionRootSpace(
Target, GetPositionRootSpace(transform));
96 var current = transform;
99 for (var i =
Bones.Length - 1; i >= 0; i--)
104 if (i ==
Bones.Length - 1)
117 current = current.parent;
127 private void ResolveIK()
142 for (
int i = 0; i <
Bones.Length; i++)
144 var targetPosition =
new Vector3();
147 targetPosition =
Vector3.Lerp(GetPositionRootSpace(
Target), GetPositionRootSpace(
Sphere), 0.6f);
151 targetPosition =
Vector3.Lerp(GetPositionRootSpace(
Target), GetPositionRootSpace(transform), 0.6f);
155 var targetRotation = GetRotationRootSpace(
Target);
161 var direction = (targetPosition -
Positions[0]).normalized;
163 for (
int i = 1; i <
Positions.Length; i++)
168 for (
int i = 0; i <
Positions.Length - 1; i++)
171 for (
int iteration = 0; iteration <
Iterations; iteration++)
175 for (
int i =
Positions.Length - 1; i > 0; i--)
184 for (
int i = 1; i <
Positions.Length; i++)
196 var polePosition = GetPositionRootSpace(
Pole);
197 for (
int i = 1; i <
Positions.Length - 1; i++)
200 var projectedPole = plane.ClosestPointOnPlane(polePosition);
201 var projectedBone = plane.ClosestPointOnPlane(
Positions[i]);
209 for (
int i = 0; i <
Positions.Length; i++)
243 private Vector3 GetPositionRootSpace(Transform current)
246 return current.position;
251 private void SetPositionRootSpace(Transform current, Vector3 position)
259 private Quaternion GetRotationRootSpace(Transform current)
263 return current.rotation;
268 private void SetRotationRootSpace(Transform current, Quaternion rotation)
271 current.rotation = rotation;
273 current.rotation =
Root.rotation * rotation;
279 var current = this.transform;
280 for (
int i = 0; i <
ChainLength && current !=
null && current.parent !=
null; i++)
282 var scale =
Vector3.Distance(current.position, current.parent.position) * 0.1f;
283 Handles.matrix = Matrix4x4.TRS(current.position,
Quaternion.FromToRotation(
Vector3.up, current.parent.position - current.position),
new Vector3(scale,
Vector3.Distance(current.parent.position, current.position), scale));
284 Handles.color =
Color.green;
286 current = current.parent;
float Delta
Distance when the solver stops
int Iterations
Solver iterations per update
Quaternion[] StartRotationBone
float SnapBackStrength
Strength of going back to the start position.
int ChainLength
Chain length of bones
Quaternion StartRotationTarget
Transform Target
Target the chain should bent to
Vector3[] StartDirectionSucc