Skip to content

Instantly share code, notes, and snippets.

@pandr
Created March 10, 2017 22:15
Show Gist options
  • Save pandr/d47a3fd6d65638485ab3fa973eb30159 to your computer and use it in GitHub Desktop.
Save pandr/d47a3fd6d65638485ab3fa973eb30159 to your computer and use it in GitHub Desktop.
Snip about feet IK for Unity
// somewhere in your charactercontroller
public bool m_UseFeetIK = true;
public float m_FeetIkOffset = 0.0f;
void OnAnimatorIK()
{
if (m_Animator)
{
if (m_IsGrounded && m_UseFeetIK)
{
var lf = m_Animator.GetBoneTransform(HumanBodyBones.LeftFoot);
var rf = m_Animator.GetBoneTransform(HumanBodyBones.RightFoot);
RaycastHit lhit;
RaycastHit rhit;
if (Physics.Raycast(lf.position + Vector3.up * 0.25f, Vector3.down, out lhit, 0.5f))
{
m_Animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
m_Animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1);
m_Animator.SetIKPosition(AvatarIKGoal.LeftFoot, lhit.point + Vector3.up * m_FeetIkOffset);
m_Animator.SetIKRotation(AvatarIKGoal.LeftFoot, Quaternion.FromToRotation(Vector3.up, lhit.normal) * transform.rotation);
}
if (Physics.Raycast(rf.position + Vector3.up * 0.25f, Vector3.down, out rhit, 1.0f))
{
m_Animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1);
m_Animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
m_Animator.SetIKPosition(AvatarIKGoal.RightFoot, rhit.point + Vector3.up * m_FeetIkOffset);
m_Animator.SetIKRotation(AvatarIKGoal.RightFoot, Quaternion.FromToRotation(Vector3.up, rhit.normal) * transform.rotation);
}
}
else
{
m_Animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 0);
m_Animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 0);
m_Animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 0);
m_Animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment