Grounder.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Dedicated abstrac base component for the Grounding solver.
  6. /// </summary>
  7. public abstract class Grounder: MonoBehaviour {
  8. #region Main Interface
  9. /// <summary>
  10. /// The master weight. Use this to fade in/out the grounding effect.
  11. /// </summary>
  12. [Tooltip("The master weight. Use this to fade in/out the grounding effect.")]
  13. [Range(0f, 1f)] public float weight = 1f;
  14. /// <summary>
  15. /// The %Grounding solver. Not to confuse with IK solvers.
  16. /// </summary>
  17. [Tooltip("The Grounding solver. Not to confuse with IK solvers.")]
  18. public Grounding solver = new Grounding();
  19. /// <summary>
  20. /// Delegate for Grounder events.
  21. /// </summary>
  22. public delegate void GrounderDelegate();
  23. /// <summary>
  24. /// Called before the Grounder updates it's solver.
  25. /// </summary>
  26. public GrounderDelegate OnPreGrounder;
  27. /// <summary>
  28. /// Called after the Grounder has updated it's solver and before the IK is applied.
  29. /// </summary>
  30. public GrounderDelegate OnPostGrounder;
  31. /// <summary>
  32. /// Resets this Grounder so characters can be teleported instananeously.
  33. /// </summary>
  34. public abstract void ResetPosition();
  35. #endregion Main Interface
  36. public bool initiated { get; protected set; }
  37. // Gets the spine bend direction
  38. protected Vector3 GetSpineOffsetTarget() {
  39. Vector3 sum = Vector3.zero;
  40. for (int i = 0; i < solver.legs.Length; i++) {
  41. sum += GetLegSpineBendVector(solver.legs[i]);
  42. }
  43. return sum;
  44. }
  45. // Logs the warning if no other warning has beed logged in this session.
  46. protected void LogWarning(string message) {
  47. Warning.Log(message, transform);
  48. }
  49. // Gets the bend direction for a foot
  50. private Vector3 GetLegSpineBendVector(Grounding.Leg leg) {
  51. Vector3 spineTangent = GetLegSpineTangent(leg);
  52. float dotF = (Vector3.Dot(solver.root.forward, spineTangent.normalized) + 1) * 0.5f;
  53. float w = (leg.IKPosition - leg.transform.position).magnitude;
  54. return spineTangent * w * dotF;
  55. }
  56. // Gets the direction from the root to the foot (ortho-normalized to root.up)
  57. private Vector3 GetLegSpineTangent(Grounding.Leg leg) {
  58. Vector3 tangent = leg.transform.position - solver.root.position;
  59. if (!solver.rotateSolver || solver.root.up == Vector3.up) return new Vector3(tangent.x, 0f, tangent.z);
  60. Vector3 normal = solver.root.up;
  61. Vector3.OrthoNormalize(ref normal, ref tangent);
  62. return tangent;
  63. }
  64. // Open the User Manual url
  65. protected abstract void OpenUserManual();
  66. // Open the Script Reference url
  67. protected abstract void OpenScriptReference();
  68. }
  69. }