/// <summary> /// given the end joint and the IK link length, setup the IK link /// </summary> public void SetBones(Transform endJoint, int len) { Dbg.Assert(len > 0, "BaseIKSolver.SetBones: the link length must be larger than 0"); ClearBones(); // the endJoint is the joint user moves, the link's last joint is its parent Transform joint = endJoint; float totalLen = 0; // add the endJoint JointInfo endInfo = new JointInfo(); endInfo.joint = endJoint; endInfo.boneLen = 0; endInfo.remainLen = 0; m_Joints.Add(endInfo); // add joint from the end to the head for (int idx = 0; idx < len; ++idx) { Transform parentJoint = joint.parent; Dbg.Assert(parentJoint != null, "BaseIKSolver.SetBones: the link length is too big, there is already no parent joint for: {0}", joint); JointInfo info = new JointInfo(); info.joint = parentJoint; info.boneLen = (parentJoint.position - joint.position).magnitude; info.remainLen = totalLen; totalLen += info.boneLen; m_Joints.Add(info); joint = parentJoint; } m_Joints.Reverse(); //reverse the array to make the most significant joint at first entry m_TransArr = new Transform[m_Joints.Count]; for (int i = 0; i < m_TransArr.Length; ++i) { m_TransArr[i] = m_Joints[i].joint; } }