OffsetEffector.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// Custom positionOffset effector for FBBIK, could be used for example to make a spine or pelvis effector.
  7. /// </summary>
  8. public class OffsetEffector : OffsetModifier {
  9. [System.Serializable]
  10. public class EffectorLink {
  11. public FullBodyBipedEffector effectorType;
  12. public float weightMultiplier = 1f;
  13. [HideInInspector] public Vector3 localPosition;
  14. }
  15. public EffectorLink[] effectorLinks;
  16. protected override void Start() {
  17. base.Start();
  18. // Store the default positions of the effectors relative to this GameObject's position
  19. foreach (EffectorLink e in effectorLinks) {
  20. e.localPosition = transform.InverseTransformPoint(ik.solver.GetEffector(e.effectorType).bone.position);
  21. // If we are using the body effector, make sure it does not change the thigh effectors
  22. if (e.effectorType == FullBodyBipedEffector.Body) ik.solver.bodyEffector.effectChildNodes = false;
  23. }
  24. }
  25. protected override void OnModifyOffset() {
  26. // Update the effectors
  27. foreach (EffectorLink e in effectorLinks) {
  28. // Using effector positionOffset
  29. Vector3 positionTarget = transform.TransformPoint(e.localPosition);
  30. ik.solver.GetEffector(e.effectorType).positionOffset += (positionTarget - (ik.solver.GetEffector(e.effectorType).bone.position + ik.solver.GetEffector(e.effectorType).positionOffset)) * weight * e.weightMultiplier;
  31. }
  32. }
  33. }
  34. }