BodyTilt.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Procedural body tilting with FBBIK.
  6. /// </summary>
  7. public class BodyTilt: OffsetModifier {
  8. [Tooltip("Speed of tilting")]
  9. public float tiltSpeed = 6f;
  10. [Tooltip("Sensitivity of tilting")]
  11. public float tiltSensitivity = 0.07f;
  12. [Tooltip("The OffsetPose components")]
  13. public OffsetPose poseLeft, poseRight;
  14. private float tiltAngle;
  15. private Vector3 lastForward;
  16. protected override void Start() {
  17. base.Start();
  18. // Store current character forward axis and Time
  19. lastForward = transform.forward;
  20. }
  21. // Called by IKSolverFullBody before updating
  22. protected override void OnModifyOffset() {
  23. // Calculate the angular delta in character rotation
  24. Quaternion change = Quaternion.FromToRotation(lastForward, transform.forward);
  25. float deltaAngle = 0;
  26. Vector3 axis = Vector3.zero;
  27. change.ToAngleAxis(out deltaAngle, out axis);
  28. if (axis.y > 0) deltaAngle = -deltaAngle;
  29. deltaAngle *= tiltSensitivity * 0.01f;
  30. deltaAngle /= deltaTime;
  31. deltaAngle = Mathf.Clamp(deltaAngle, -1f, 1f);
  32. tiltAngle = Mathf.Lerp(tiltAngle, deltaAngle, deltaTime * tiltSpeed);
  33. // Applying positionOffsets
  34. float tiltF = Mathf.Abs(tiltAngle) / 1f;
  35. if (tiltAngle < 0) poseRight.Apply(ik.solver, tiltF);
  36. else poseLeft.Apply(ik.solver, tiltF);
  37. // Store current character forward axis and Time
  38. lastForward = transform.forward;
  39. }
  40. }
  41. }