x


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?

more ▼

asked Feb 05 '10 at 09:42 AM

runevision gravatar image

runevision ♦♦
8.8k 32 49 112

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Feb 05 '10 at 09:48 AM

runevision gravatar image

runevision ♦♦
8.8k 32 49 112

What do you recommend if the characters have more than 2 limbs? Reducing the number of limbs? Is there any kind of work around?

Jun 02 '10 at 04:01 PM Arbbot

I don't have any recommendations or know of any workarounds if the crude solver in the Locomotion System is not adequate.

Jun 03 '10 at 01:21 PM runevision ♦♦

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! mine is 2D and has a shit ton of comments ;)

Apr 11 '12 at 02:25 PM davebuchhofer
(comments are locked)
10|3000 characters needed characters left

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

http://u3d.as/content/dogzer/inverse-kinematics/2fP

more ▼

answered Sep 10 '11 at 03:07 AM

caitlyn gravatar image

caitlyn
16

(comments are locked)
10|3000 characters needed characters left

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;
}
more ▼

answered Jul 28 '12 at 07:51 AM

EnsurdFrndship gravatar image

EnsurdFrndship
16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5668
x399
x67

asked: Feb 05 '10 at 09:42 AM

Seen: 23416 times

Last Updated: Nov 22 '12 at 07:41 PM