1
0

IK.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. public class IK
  3. {
  4. private TBody body;
  5. private Quaternion defHipQ;
  6. private Quaternion defKneeQ;
  7. private float defLEN1;
  8. private float defLEN2;
  9. private Vector3 knee_old;
  10. private Vector3 vechand;
  11. public void Init(Transform hip, Transform knee, Transform ankle, TBody b)
  12. {
  13. body = b;
  14. defLEN1 = (hip.position - knee.position).magnitude;
  15. defLEN2 = (ankle.position - knee.position).magnitude;
  16. knee_old = knee.position;
  17. defHipQ = hip.localRotation;
  18. defKneeQ = knee.localRotation;
  19. vechand = Vector3.zero;
  20. }
  21. public void Porc(Transform hip, Transform knee, Transform ankle, Vector3 tgt, Vector3 vechand_offset)
  22. {
  23. knee_old = knee_old * 0.5f + knee.position * 0.5f;
  24. Vector3 normalized1 = (knee_old - tgt).normalized;
  25. knee_old = tgt + normalized1 * defLEN2;
  26. Vector3 normalized2 = (knee_old - hip.position).normalized;
  27. knee_old = hip.position + normalized2 * defLEN1;
  28. new Quaternion().SetLookRotation(normalized2);
  29. hip.transform.rotation =
  30. Quaternion.FromToRotation(knee.transform.position - hip.transform.position, knee_old - hip.transform.position)
  31. * hip.transform.rotation;
  32. knee.transform.rotation =
  33. Quaternion.FromToRotation(ankle.transform.position - knee.transform.position, tgt - knee.transform.position)
  34. * knee.transform.rotation;
  35. }
  36. }