# How to do inverse kinematics (IK) in Unity?

Inverse kinematics (IK) is a way to automatically calculate the joint angles of e.g. a leg or arm based on where the end effector (typically the foot or hand) should be. So you could specify that the hand of a person should be at some specific position, and IK kan then be used to calculate the joint angles of the shoulder and elbow in order to make the hand be there.

Inverse kinematics need special algorithms to "solve" the problem of finding the right joint angles. This is not trivial, especially if a limb has more than two segments. (A human leg would have two segments while a horse hind leg would have three.)

Are there any solutions for doing IK in Unity?

• For limbs with two segments?

• For limbs with more than two segments?

• Any solutions where it's possible to specify constraints for the joints so unnatural poses can be better avoided?

For two segment IK, an easy solution is to use Dogzer's Inverse Kinematics solver which is available on the Asset Store for free:

For more than two segment inverse kinematics, I don't know of a great solution. The Locomotion System includes some IK solvers in order to do foot placement on uneven terrain. It also has a solver for limbs with more than two segments that works okay sometimes, but is not that great. Sometimes it can produce unnatural looking poses.

If a better solver for more than two segments is needed, someone needs to implement it. There currently is no good solution for this; if there is, please add an answer below.

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();
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++) {
``````

}

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].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++) {
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);

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++) {
}

//Scales
var scales : List. = new List.();
scales.Clear();

for (i = 0; i < pLimbs.Count; i++) {
}

//Position
var positions : List. = new List.();
positions.Clear();

for (i = 0; i < pLimbs.Count; i++) {
}

//Position
var rotations : List. = new List.();
rotations.Clear();

for (i = 0; i < pLimbs.Count; i++) {
}

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++) {
}
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;
}

You might find Dogzer’s easy-to-use IK solver, on the asset store, to be very helpful…

RootMotion’s FinalIK (\$90) is highly recommended.
For fast and easy prototyping / integration, its hard to beat SimpleIK – a +steal+ at \$2, by Takohi Games.
Both are available on the Asset Store.

FinalIK