RotationLimitHinge.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// The hinge rotation limit limits the rotation to 1 degree of freedom around Axis. This rotation limit is additive which means the limits can exceed 360 degrees.
  6. /// </summary>
  7. [HelpURL("http://www.root-motion.com/finalikdox/html/page14.html")]
  8. [AddComponentMenu("Scripts/RootMotion.FinalIK/Rotation Limits/Rotation Limit Hinge")]
  9. public class RotationLimitHinge : RotationLimit {
  10. // Open the User Manual URL
  11. [ContextMenu("User Manual")]
  12. private void OpenUserManual() {
  13. Application.OpenURL("http://www.root-motion.com/finalikdox/html/page14.html");
  14. }
  15. // Open the Script Reference URL
  16. [ContextMenu("Scrpt Reference")]
  17. private void OpenScriptReference() {
  18. Application.OpenURL("http://www.root-motion.com/finalikdox/html/class_root_motion_1_1_final_i_k_1_1_rotation_limit_hinge.html");
  19. }
  20. // Link to the Final IK Google Group
  21. [ContextMenu("Support Group")]
  22. void SupportGroup() {
  23. Application.OpenURL("https://groups.google.com/forum/#!forum/final-ik");
  24. }
  25. // Link to the Final IK Asset Store thread in the Unity Community
  26. [ContextMenu("Asset Store Thread")]
  27. void ASThread() {
  28. Application.OpenURL("http://forum.unity3d.com/threads/final-ik-full-body-ik-aim-look-at-fabrik-ccd-ik-1-0-released.222685/");
  29. }
  30. #region Main Interface
  31. /// <summary>
  32. /// Should the rotation be limited around the axis?
  33. /// </summary>
  34. public bool useLimits = true;
  35. /// <summary>
  36. /// The min limit around the axis.
  37. /// </summary>
  38. public float min = -45;
  39. /// <summary>
  40. /// The max limit around the axis.
  41. /// </summary>
  42. public float max = 90;
  43. #endregion Main Interface
  44. /*
  45. * Limits the rotation in the local space of this instance's Transform.
  46. * */
  47. protected override Quaternion LimitRotation(Quaternion rotation) {
  48. return LimitHinge(rotation);
  49. }
  50. [HideInInspector] public float zeroAxisDisplayOffset; // Angular offset of the scene view display of the Hinge rotation limit
  51. private float lastAngle;
  52. /*
  53. * Apply the hinge rotation limit
  54. * */
  55. private Quaternion LimitHinge(Quaternion rotation) {
  56. // If limit is zero return rotation fixed to axis
  57. if (min == 0 && max == 0 && useLimits) return Quaternion.AngleAxis(0, axis);
  58. // Get 1 degree of freedom rotation along axis
  59. Quaternion free1DOF = Limit1DOF(rotation, axis);
  60. if (!useLimits) return free1DOF;
  61. Quaternion workingSpace = Quaternion.Inverse(Quaternion.AngleAxis(lastAngle, axis) * Quaternion.LookRotation(secondaryAxis, axis));
  62. Vector3 d = workingSpace * free1DOF * secondaryAxis;
  63. float deltaAngle = Mathf.Atan2(d.x, d.z) * Mathf.Rad2Deg;
  64. lastAngle = Mathf.Clamp(lastAngle + deltaAngle, min, max);
  65. return Quaternion.AngleAxis(lastAngle, axis);
  66. }
  67. }
  68. }