I altered the code a little so that you can put the ik script directly onto the parent limb. Then, the code adds the ik script to its children, grand-children etc. All you have to do to the ik script on the parent limb is set the target and set the reach limb. The reach limb is the hand. The target is the target object the hand must reach. You can leave the elbow target empty if you like. It will just base it on Vector3(0,0,0), and it will do ik for more than just 2 limbs. Here is the code…
import System.Collections.Generic;
@HideInInspector
var pLimbs : List.;
var target : Transform;
var pReachLimb : Transform;
var elbowTarget : Transform;
var IsEnabled : boolean = true;
var debug : boolean = true;
var transition : float = 1.0;
private var startRotations : List.;
private var targetRelativeStartPosition : Vector3;
private var elbowTargetRelativeStartPosition : Vector3;
private var pIk : GameObject;
function Start(){
pLimbs.Clear();
pLimbs.Add(transform);
pLimbs.Add(pReachLimb);
pLimbs.Add(pReachLimb);
while (pLimbs[1] != null && pLimbs[1].parent.transform != pLimbs[0]) {
pLimbs[1] = pLimbs[1].parent.transform;
}
pIk = new GameObject(name+"-ik");
pIk.transform.parent = null;
var i : int = 0;
startRotations = new List.();
startRotations.Clear();
for (i = 0; i < pLimbs.Count; i++) {
startRotations.Add(pLimbs*.rotation);*
}
targetRelativeStartPosition = target.position - pLimbs[0].position;
if (elbowTarget != null) {
elbowTargetRelativeStartPosition = elbowTarget.position - pLimbs[0].position;
} else {
elbowTargetRelativeStartPosition = Vector3(0,0,0);
}
}
function LateUpdate () {
if (!IsEnabled){
return;
}
CalculateIK2();
}
function CalculateIK2(){
//Lengths and distances.
var i : int = 0;
var lengths : List. = new List.();
lengths.Clear();
var totalLength : float = 0;
var distance : float = Vector3.Distance(pLimbs[0].position, pLimbs[pLimbs.Count-1].position);
pIk.transform.parent = null;
pLimbs[0] = transform;
pLimbs[1] = pReachLimb;
pLimbs[2] = pReachLimb;
while (pLimbs[1] != null && pLimbs[1].parent.transform != pLimbs[0]) {
pLimbs[1] = pLimbs[1].parent.transform;
}
if (pLimbs[2].parent.transform != pLimbs[1]) {
if (!pLimbs[1].GetComponent(“ikLimb”)) {
pLimbs[1].gameObject.AddComponent(“ikLimb”);
pLimbs[1].GetComponent.().pLimbs = new List.();
pLimbs[1].GetComponent.().pLimbs.Clear();
}
pLimbs[1].GetComponent.().target = target;
pLimbs[1].GetComponent.().elbowTarget = elbowTarget;
pLimbs[1].GetComponent.().IsEnabled = IsEnabled;
pLimbs[1].GetComponent.().debug = debug;
pLimbs[1].GetComponent.().transition = transition;
pLimbs[1].GetComponent.().pReachLimb = pReachLimb;
}
for (i = 0; i < pLimbs.Count-1; i++) {
lengths.Add(Vector3.Distance(pLimbs*.position, pLimbs[i+1].position));*
totalLength += Vector3.Distance(pLimbs*.position, pLimbs[i+1].position);*
}
distance = Mathf.Min(distance, totalLength-0.0001);
var adj : float = (Mathf.Pow(lengths[0],2) - Mathf.Pow(lengths[1],2) + Mathf.Pow(distance,2))/(2*distance);
//Debug.Log(adj);
var ikAng : float = Mathf.Acos(adj/lengths[0]) * Mathf.Rad2Deg;
var targetPos : Vector3 = target.position;
var elbowPos : Vector3;
if (elbowTarget != null) {
elbowPos = elbowTarget.position;
} else {
elbowPos = Vector3(0,0,0);
}
//Parents
var parents : List. = new List.();
parents.Clear();
for (i = 0; i < pLimbs.Count; i++) {
parents.Add(pLimbs*.parent);*
}
//Scales
var scales : List. = new List.();
scales.Clear();
for (i = 0; i < pLimbs.Count; i++) {
scales.Add(pLimbs*.localScale);*
}
//Position
var positions : List. = new List.();
positions.Clear();
for (i = 0; i < pLimbs.Count; i++) {
positions.Add(pLimbs*.localPosition);*
}
//Position
var rotations : List. = new List.();
rotations.Clear();
for (i = 0; i < pLimbs.Count; i++) {
rotations.Add(pLimbs*.rotation);*
}
target.position = targetRelativeStartPosition + pLimbs[0].position;
if (elbowTarget != null) {
elbowTarget.position = elbowTargetRelativeStartPosition + pLimbs[0].position;
}
for (i = 0; i < pLimbs.Count; i++) {
pLimbs_.rotation = startRotations*;
}*_
pIk.transform.position = pLimbs[0].position;
pIk.transform.LookAt(targetPos, elbowPos - pIk.transform.position);
var axisCorrections : List. = List.();
axisCorrections.Clear();
for (i = 0; i < pLimbs.Count; i++) {
axisCorrections.Add(new GameObject(pLimbs*.name+“AxisCorrection”));*
}
for (i = 0; i < pLimbs.Count; i++) {
axisCorrections_.transform.position = pLimbs*.position;
if (i != pLimbs.Count-1) {_
_axisCorrections.transform.LookAt(pLimbs[i+1].position, pIk.transform.root.up);
}
if (i == 0) {_
_axisCorrections.transform.parent = pIk.transform;
} else {_
_axisCorrections.transform.parent = axisCorrections[i-1].transform;
}
pLimbs.parent = axisCorrections.transform;
}*_
target.position = targetPos;
if (elbowTarget != null) {
elbowTarget.position = elbowPos;
}
if (elbowTarget != null) {
axisCorrections[0].transform.LookAt(target,elbowTarget.position - axisCorrections[0].transform.position);
axisCorrections[0].transform.localRotation.eulerAngles.x -= ikAng;
axisCorrections[1].transform.LookAt(target,elbowTarget.position - axisCorrections[0].transform.position);
axisCorrections[2].transform.rotation = target.rotation;
} else {
axisCorrections[0].transform.LookAt(target, -axisCorrections[0].transform.position);
axisCorrections[0].transform.localRotation.eulerAngles.x -= ikAng;
axisCorrections[1].transform.LookAt(target, -axisCorrections[0].transform.position);
axisCorrections[2].transform.rotation = target.rotation;
}
for (i = 0; i < pLimbs.Count; i++) {
pLimbs_.parent = parents*;
pLimbs.localScale = scales;
pLimbs.localPosition = positions;
}*_
for (i = 0; i < pLimbs.Count; i++) {
Destroy(axisCorrections*);*
}
transition = Mathf.Clamp01(transition);
for (i = 0; i < pLimbs.Count; i++) {
pLimbs.rotation = Quaternion.Slerp(rotations_, pLimbs*.rotation, transition);
}*_
pIk.transform.parent = transform;
}