IKExecutionOrder.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Manages the execution order of IK components.
  6. /// </summary>
  7. public class IKExecutionOrder : MonoBehaviour {
  8. /// <summary>
  9. /// The IK components, assign in the order in which you wish to update them.
  10. /// </summary>
  11. [Tooltip("The IK components, assign in the order in which you wish to update them.")]
  12. public IK[] IKComponents;
  13. [Tooltip("Optional. Assign it if you are using 'Animate Physics' as the Update Mode.")]
  14. /// <summary>
  15. /// Optional. Assign it if you are using 'Animate Physics' as the Update Mode.
  16. /// </summary>
  17. public Animator animator;
  18. private bool fixedFrame;
  19. private bool animatePhysics {
  20. get {
  21. if (animator == null) return false;
  22. return animator.updateMode == AnimatorUpdateMode.AnimatePhysics;
  23. }
  24. }
  25. // Disable the IK components
  26. void Start() {
  27. for (int i = 0; i < IKComponents.Length; i++) IKComponents[i].enabled = false;
  28. }
  29. // Fix Transforms in Normal update mode
  30. void Update() {
  31. if (animatePhysics) return;
  32. FixTransforms ();
  33. }
  34. // Fix Transforms in Animate Physics update mode
  35. void FixedUpdate() {
  36. fixedFrame = true;
  37. if (animatePhysics) FixTransforms ();
  38. }
  39. // Update the IK components in a specific order
  40. void LateUpdate() {
  41. if (!animatePhysics || fixedFrame) {
  42. for (int i = 0; i < IKComponents.Length; i++) {
  43. IKComponents [i].GetIKSolver ().Update ();
  44. }
  45. fixedFrame = false;
  46. }
  47. }
  48. private void FixTransforms() {
  49. for (int i = 0; i < IKComponents.Length; i++) {
  50. if (IKComponents[i].fixTransforms) IKComponents[i].GetIKSolver().FixTransforms();
  51. }
  52. }
  53. }
  54. }