MotionAbsorb.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// Absorbing motion on FBBIK effectors on impact. Attach this to the GameObject that receives OnCollisionEnter calls.
  7. /// </summary>
  8. public class MotionAbsorb : OffsetModifier {
  9. [System.Serializable]
  10. public enum Mode {
  11. Position,
  12. PositionOffset
  13. }
  14. // Manages motion absorbing for an effector
  15. [System.Serializable]
  16. public class Absorber {
  17. [Tooltip("The type of effector (hand, foot, shoulder...) - this is just an enum")]
  18. public FullBodyBipedEffector effector;
  19. [Tooltip("How much should motion be absorbed on this effector")]
  20. public float weight = 1f;
  21. private Vector3 position;
  22. private Quaternion rotation = Quaternion.identity;
  23. private IKEffector e;
  24. // Set effector position and rotation to match it's bone
  25. public void SetToBone(IKSolverFullBodyBiped solver, Mode mode) {
  26. e = solver.GetEffector(effector);
  27. // Using world space position and rotation here for the sake of simplicity of the demo
  28. // Ideally we should use position and rotation relative to character's root, so we could move around while doing this.
  29. switch(mode) {
  30. case Mode.Position:
  31. e.position = e.bone.position;
  32. e.rotation = e.bone.rotation;
  33. return;
  34. case Mode.PositionOffset:
  35. position = e.bone.position;
  36. rotation = e.bone.rotation;
  37. return;
  38. }
  39. }
  40. // Set effector position and rotation weight to match the value, multiply with the weight of this Absorber
  41. public void UpdateEffectorWeights(float w) {
  42. e.positionWeight = w * weight;
  43. e.rotationWeight = w * weight;
  44. }
  45. // Set effector positionOffset to match the position, multiply with the weight of this Absorber
  46. public void SetPosition(float w) {
  47. e.positionOffset += (position - e.bone.position) * w * weight;
  48. }
  49. // Set effector bone rotation to match the rotation, multiply with the weight of this Absorber
  50. public void SetRotation(float w) {
  51. e.bone.rotation = Quaternion.Slerp(e.bone.rotation, rotation, w * weight);
  52. }
  53. }
  54. [Tooltip("Use either effector position, position weight, rotation, rotationWeight or positionOffset and rotating the bone directly.")]
  55. public Mode mode;
  56. [Tooltip("Array containing the absorbers")]
  57. public Absorber[] absorbers;
  58. [Tooltip("Weight falloff curve (how fast will the effect reduce after impact)")]
  59. public AnimationCurve falloff;
  60. [Tooltip("How fast will the impact fade away. (if 1, effect lasts for 1 second)")]
  61. public float falloffSpeed = 1f;
  62. private float timer; // Used for fading out the effect of the impact
  63. private float w;
  64. private Mode initialMode;
  65. protected override void Start() {
  66. base.Start();
  67. ik.solver.OnPostUpdate += AfterIK;
  68. initialMode = mode;
  69. }
  70. void OnCollisionEnter(Collision c) {
  71. // Don't register another contact until the effect of the last one has faded
  72. if (timer > 0f) return;
  73. // Reset timer
  74. timer = 1f;
  75. // Set effector position and rotation to match it's bone
  76. for (int i = 0; i < absorbers.Length; i++) absorbers[i].SetToBone(ik.solver, mode);
  77. }
  78. // Called by IKSolverFullBody before updating
  79. protected override void OnModifyOffset() {
  80. if (timer <= 0f) return;
  81. mode = initialMode;
  82. // Fading out the effect
  83. timer -= Time.deltaTime * falloffSpeed;
  84. // Evaluate the absorb weight
  85. w = falloff.Evaluate(timer);
  86. // Set the weights of the effectors
  87. if (mode == Mode.Position) {
  88. for (int i = 0; i < absorbers.Length; i++) absorbers[i].UpdateEffectorWeights(w * weight);
  89. } else {
  90. for (int i = 0; i < absorbers.Length; i++) absorbers[i].SetPosition(w * weight);
  91. }
  92. }
  93. void AfterIK() {
  94. if (timer <= 0f) return;
  95. if (mode == Mode.Position) return;
  96. for (int i = 0; i < absorbers.Length; i++) absorbers[i].SetRotation(w * weight);
  97. }
  98. protected override void OnDestroy() {
  99. base.OnDestroy();
  100. if (ik != null) ik.solver.OnPostUpdate -= AfterIK;
  101. }
  102. }
  103. }