- Home /
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?
Answer by runevision · Feb 05, 2010 at 09:48 AM
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.
What do you recommend if the characters have more than 2 limbs? Reducing the number of limbs? Is there any kind of work around?
I don't have any recommendations or know of any workarounds if the crude solver in the Locomotion System is not adequate.
Another one is here: http://buchhofer.com/2012/01/configurable-inverse-kinematics-in-unity-at-runtime/ its a very basic CCD system based on an old article in gdmag, limited to 2D for my case, but works with an arbitrary number of joints
Edit: looks like the same port as mikriloulou's post below, his is 3D with dof axis restrictions! $$anonymous$$e is 2D and has a shit ton of comments ;)
Answer by EnsurdFrndship · Jul 28, 2012 at 07:51 AM
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[i].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[i].position, pLimbs[i+1].position));
totalLength += Vector3.Distance(pLimbs[i].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[i].parent);
}
//Scales
var scales : List. = new List.();
scales.Clear();
for (i = 0; i < pLimbs.Count; i++) {
scales.Add(pLimbs[i].localScale);
}
//Position
var positions : List. = new List.();
positions.Clear();
for (i = 0; i < pLimbs.Count; i++) {
positions.Add(pLimbs[i].localPosition);
}
//Position
var rotations : List. = new List.();
rotations.Clear();
for (i = 0; i < pLimbs.Count; i++) {
rotations.Add(pLimbs[i].rotation);
}
target.position = targetRelativeStartPosition + pLimbs[0].position;
if (elbowTarget != null) {
elbowTarget.position = elbowTargetRelativeStartPosition + pLimbs[0].position;
}
for (i = 0; i < pLimbs.Count; i++) {
pLimbs[i].rotation = startRotations[i];
}
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[i].name+"AxisCorrection"));
}
for (i = 0; i < pLimbs.Count; i++) {
axisCorrections[i].transform.position = pLimbs[i].position;
if (i != pLimbs.Count-1) {
axisCorrections[i].transform.LookAt(pLimbs[i+1].position, pIk.transform.root.up);
}
if (i == 0) {
axisCorrections[i].transform.parent = pIk.transform;
} else {
axisCorrections[i].transform.parent = axisCorrections[i-1].transform;
}
pLimbs[i].parent = axisCorrections[i].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[i].parent = parents[i];
pLimbs[i].localScale = scales[i];
pLimbs[i].localPosition = positions[i];
}
for (i = 0; i < pLimbs.Count; i++) {
Destroy(axisCorrections[i]);
}
transition = Mathf.Clamp01(transition);
for (i = 0; i < pLimbs.Count; i++) {
pLimbs[i].rotation = Quaternion.Slerp(rotations[i], pLimbs[i].rotation, transition);
}
pIk.transform.parent = transform;
}
Answer by caitlyn · Sep 10, 2011 at 03:07 AM
You might find Dogzer's easy-to-use IK solver, on the asset store, to be very helpful...
Answer by mikriloulou · Nov 16, 2011 at 12:22 PM
Also check this out: http://kimkijeung.com/2011/02/10/unity-inverse-kinematics-solution/comment-page-1/#comment-289
Answer by gregroberts · Feb 28, 2015 at 02:47 PM
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.
SimpleIK http://www.takohi.com/use-simple-ik-on-unity/
FinalIK https://www.assetstore.unity3d.com/en/#!/content/14290
Your answer
Follow this Question
Related Questions
Inverse Kinematics On Animation 0 Answers
Adjusting humanoid character animations (Procedural animations) 0 Answers
Do I need Inverse Kinematics? Asking for guidelines to program my own custom script 0 Answers
How can I use IK with AnimationClipPlayable or AnimationMixerPlayable? 1 Answer
Error in animation rigging 1 Answer