- Home /
Why does the IK script not work?
I have been working on my own IK script as of late but frankly the thing is giving me headache after headache. I get the mathmatical concept of how to figure the angles etc. (or I feel I do) but when it comes to implementation it just falls through. At the moment my characters arms aren't moving at all. I believe that is because the shoulderAngle Acos calculations occasionally are returning NAN (which I'm not sure how as the distToTarget clamp should rid me of that headache. Any help would be appreciated...
var shoulderPivot : Transform; // Used for determining length and where to rotate
var elbowPivot : Transform; // Used for determining length and where to rotate
var handPivot : Transform; // Used for determining length and where to rotate
var ikTarget : Transform; // This is what the arm is reaching for.
var elbowWeight : Transform; // Used to keep elbow from making wierd rotations.
var bicepLength : float; // Length of first arm, R in CCI equation
var forearmLength : float; // Length of second arm, r in CCI equation
var distToTarget : float; // Distance to target for IK.
var midPointDistance : float; // Length of X (the length from arm joint to elbow)
var shoulderAngle : float; // Store the angle that we are using to make elbow angle calc. Angle B on second link.
var handAngle : float; // Same as above.
function Awake(){
bicepLength = Vector3.Distance(shoulderPivot.position,elbowPivot.position);
forearmLength = Vector3.Distance(elbowPivot.position,handPivot.position);
}
function Update(){
distToTarget = Vector3.Distance(shoulderPivot.position,ikTarget.position);
distToTarget = Mathf.Clamp(distToTarget,0,bicepLength+forearmLength);
transform.LookAt(ikTarget,transform.position - elbowWeight.position);
midPointDistance = (Mathf.Pow(distToTarget,2)-Mathf.Pow(bicepLength,2)+Mathf.Pow(forearmLength,2))/(distToTarget * 2);
shoulderAngle = Mathf.Acos(midPointDistance/bicepLength) * Mathf.Rad2Deg;
handAngle = Mathf.Acos(distToTarget-midPointDistance/forearmLength) * Mathf.Rad2Deg;
shoulderPivot.localRotation = Quaternion.Euler(-shoulderAngle,0,0);
elbowPivot.localRotation = Quaternion.Euler(180-shoulderAngle-handAngle,0,0);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613135001im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
what is the foot ik button for 1 Answer
Inverse Kinematics in editor? 0 Answers
Using SetIKPosition the final hand position is offset from target 2 Answers
Creating an IK rig to bake into Mecanim 0 Answers
turn on/off FootIK by script 0 Answers