32 [Header(
"Solver Parameters")]
84 throw new UnityException(
"The chain value is longer than the ancestor chain!");
91 Target =
new GameObject(gameObject.name +
" Target").transform;
92 SetPositionRootSpace(
Target, GetPositionRootSpace(transform));
97 var current = transform;
100 for (var i =
Bones.Length - 1; i >= 0; i--)
105 if (i ==
Bones.Length - 1)
118 current = current.parent;
128 private void ResolveIK()
143 for (
int i = 0; i <
Bones.Length; i++)
146 var targetPosition = GetPositionRootSpace(
Target);
147 var targetRotation = GetRotationRootSpace(
Target);
153 var direction = (targetPosition -
Positions[0]).normalized;
155 for (
int i = 1; i <
Positions.Length; i++)
160 for (
int i = 0; i <
Positions.Length - 1; i++)
163 for (
int iteration = 0; iteration <
Iterations; iteration++)
167 for (
int i =
Positions.Length - 1; i > 0; i--)
176 for (
int i = 1; i <
Positions.Length; i++)
188 var polePosition = GetPositionRootSpace(
Pole);
189 for (
int i = 1; i <
Positions.Length - 1; i++)
192 var projectedPole = plane.ClosestPointOnPlane(polePosition);
193 var projectedBone = plane.ClosestPointOnPlane(
Positions[i]);
201 for (
int i = 0; i <
Positions.Length; i++)
207 SetRotationRootSpace(
Bones[i],
239 transform.forward =
Vector3.down;
244 private Vector3 GetPositionRootSpace(Transform current)
247 return current.position;
252 private void SetPositionRootSpace(Transform current, Vector3 position)
260 private Quaternion GetRotationRootSpace(Transform current)
264 return current.rotation;
269 private void SetRotationRootSpace(Transform current, Quaternion rotation)
272 current.rotation = rotation;
274 current.rotation =
Root.rotation * rotation;
280 var current = this.transform;
281 for (
int i = 0; i <
ChainLength && current !=
null && current.parent !=
null; i++)
283 var scale =
Vector3.Distance(current.position, current.parent.position) * 0.1f;
284 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));
285 Handles.color =
Color.green;
287 current = current.parent;
int Iterations
Solver iterations per update
Quaternion[] StartRotationBone
int ChainLength
Chain length of bones
Vector3[] StartDirectionSucc
float SnapBackStrength
Strength of going back to the start position.
float Delta
Distance when the solver stops
Transform Target
Target the chain should bent to
Quaternion StartRotationTarget