CharacterAnimationThirdPersonIK.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// Contols animation for a third person person controller.
  7. /// </summary>
  8. public class CharacterAnimationThirdPersonIK: CharacterAnimationThirdPerson {
  9. private FullBodyBipedIK ik;
  10. protected override void Start() {
  11. base.Start();
  12. ik = GetComponent<FullBodyBipedIK>();
  13. }
  14. protected override void LateUpdate() {
  15. base.LateUpdate();
  16. // Rotate the upper body a little bit to world up vector if the character is rotated (for wall-running)
  17. if (Vector3.Angle(transform.up, Vector3.up) <= 0.01f) return;
  18. Quaternion r = Quaternion.FromToRotation(transform.up, Vector3.up);
  19. RotateEffector(ik.solver.bodyEffector, r, 0.1f);
  20. RotateEffector(ik.solver.leftShoulderEffector, r, 0.2f);
  21. RotateEffector(ik.solver.rightShoulderEffector, r, 0.2f);
  22. RotateEffector(ik.solver.leftHandEffector, r, 0.1f);
  23. RotateEffector(ik.solver.rightHandEffector, r, 0.1f);
  24. }
  25. // Rotate an effector from the root of the character
  26. private void RotateEffector(IKEffector effector, Quaternion rotation, float mlp) {
  27. Vector3 d1 = effector.bone.position - transform.position;
  28. Vector3 d2 = rotation * d1;
  29. Vector3 offset = d2 - d1;
  30. effector.positionOffset += offset * mlp;
  31. }
  32. }
  33. }