Constraints.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Contains and manages a set of constraints.
  6. /// </summary>
  7. [System.Serializable]
  8. public class Constraints {
  9. #region Main Interface
  10. /// <summary>
  11. /// The transform.
  12. /// </summary>
  13. public Transform transform;
  14. /// <summary>
  15. /// The target.
  16. /// </summary>
  17. public Transform target;
  18. /// <summary>
  19. /// The position offset.
  20. /// </summary>
  21. public Vector3 positionOffset;
  22. /// <summary>
  23. /// The position to lerp to by positionWeight
  24. /// </summary>
  25. public Vector3 position;
  26. /// <summary>
  27. /// The weight of lerping to position
  28. /// </summary>
  29. [Range(0f, 1f)]
  30. public float positionWeight;
  31. /// <summary>
  32. /// The rotation offset.
  33. /// </summary>
  34. public Vector3 rotationOffset;
  35. /// <summary>
  36. /// The rotation to slerp to by rotationWeight
  37. /// </summary>
  38. public Vector3 rotation;
  39. /// <summary>
  40. /// The weight of slerping to rotation
  41. /// </summary>
  42. [Range(0f, 1f)]
  43. public float rotationWeight;
  44. /// <summary>
  45. /// Determines whether this instance is valid.
  46. /// </summary>
  47. public bool IsValid() {
  48. return transform != null;
  49. }
  50. /// <summary>
  51. /// Initiate to the specified transform.
  52. /// </summary>
  53. public void Initiate(Transform transform) {
  54. this.transform = transform;
  55. this.position = transform.position;
  56. this.rotation = transform.eulerAngles;
  57. }
  58. /// <summary>
  59. /// Updates the constraints.
  60. /// </summary>
  61. public void Update() {
  62. if (!IsValid()) return;
  63. // Position
  64. if (target != null) position = target.position;
  65. transform.position += positionOffset;
  66. if (positionWeight > 0f) transform.position = Vector3.Lerp(transform.position, position, positionWeight);
  67. // Rotation
  68. if (target != null) rotation = target.eulerAngles;
  69. transform.rotation = Quaternion.Euler(rotationOffset) * transform.rotation;
  70. if (rotationWeight > 0f) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotation), rotationWeight);
  71. }
  72. #endregion Main Interface
  73. }
  74. }