- Home /
Problems with IK implementation
I'm following an old tutorial for creating an IK system in Unity and have run into a big problem. I have only written the code for the feet so far, but my objects that I'm associating with the feet and knees are not moving with the object and are rather staying stationary. This causes my models legs to warp and it starts with its feet pointed the wrong way.
public class IKHandler : MonoBehaviour {
Animator anim;
public float ikWeight = 1;
public Transform leftIKTarget;
public Transform rightIKTarget;
public Transform hintLeft;
public Transform hintRight;
Vector3 lFpos;
Vector3 rFpos;
Quaternion lFrot;
Quaternion rFrot;
float lFweight;
float rFweight;
Transform leftFoot;
Transform rightFoot;
public float offsetY;
public float lookIKWeight;
public float bodyWeight;
public float headWeight;
public float eyeWeight;
public float clampWeight;
public Transform lookPos;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
//anim.gameObject.activeSelf;
leftFoot = anim.GetBoneTransform(HumanBodyBones.LeftFoot);
rightFoot = anim.GetBoneTransform(HumanBodyBones.RightFoot);
lFrot = leftFoot.rotation;
rFrot = rightFoot.rotation;
}
// Update is called once per frame
void Update ()
{
RaycastHit leftHit;
RaycastHit rightHit;
Vector3 lpos = leftFoot.TransformPoint(Vector3.zero);
Vector3 rpos = rightFoot.TransformPoint(Vector3.zero);
if(Physics.Raycast(lpos, -Vector3.up, out leftHit, 1))
{
lFpos = leftHit.point;
//lHrot = Quaternion.FromToRotation(transform.up, leftHit.normal) * transform.rotation;
}
if (Physics.Raycast(rpos, -Vector3.up, out rightHit, 1))
{
rFpos = rightHit.point;
//rHrot = Quaternion.FromToRotation(transform.up, rightHit.normal) * transform.rotation;
}
}
void OnAnimatorIK()
{
anim.SetLookAtWeight(lookIKWeight, bodyWeight, headWeight, eyeWeight, clampWeight);
anim.SetLookAtPosition(lookPos.position);
lFweight = anim.GetFloat("LeftFoot");
rFweight = anim.GetFloat("RightFoot");
lFpos = anim.GetIKPosition(AvatarIKGoal.LeftFoot);
rFpos = anim.GetIKPosition(AvatarIKGoal.RightFoot);
anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, ikWeight);
anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, ikWeight);
anim.SetIKPosition(AvatarIKGoal.LeftHand, lFpos + new Vector3(0, offsetY, 0));
anim.SetIKPosition(AvatarIKGoal.RightHand, rFpos + new Vector3(0, offsetY, 0));
anim.SetIKHintPositionWeight(AvatarIKHint.LeftKnee, ikWeight);
anim.SetIKHintPositionWeight(AvatarIKHint.RightKnee, ikWeight);
anim.SetIKHintPosition(AvatarIKHint.LeftKnee, hintLeft.position);
anim.SetIKHintPosition(AvatarIKHint.RightKnee, hintRight.position);
anim.SetIKRotationWeight(AvatarIKGoal.LeftFoot, ikWeight);
anim.SetIKRotationWeight(AvatarIKGoal.RightFoot, ikWeight);
anim.SetIKRotation(AvatarIKGoal.LeftFoot, lFrot);
anim.SetIKRotation(AvatarIKGoal.RightFoot, rFrot);
}
}
[1]: /storage/temp/128702-screenshot-6.png
screenshot-7.png
(189.4 kB)
screenshot-6.png
(347.4 kB)
Comment