InteractionLookAt.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion;
  4. namespace RootMotion.FinalIK {
  5. /// <summary>
  6. /// Controls LookAtIK for the InteractionSystem
  7. /// </summary>
  8. [System.Serializable]
  9. public class InteractionLookAt {
  10. // (Optional) reference to the LookAtIK component that will be used to make the character look at the objects that it is interacting with.
  11. [Tooltip("(Optional) reference to the LookAtIK component that will be used to make the character look at the objects that it is interacting with.")]
  12. public LookAtIK ik;
  13. /// <summary>
  14. /// Interpolation speed of the LookAtIK target.
  15. /// </summary>
  16. [Tooltip("Interpolation speed of the LookAtIK target.")]
  17. public float lerpSpeed = 5f;
  18. /// <summary>
  19. /// Interpolation speed of the LookAtIK weight.
  20. /// </summary>
  21. [Tooltip("Interpolation speed of the LookAtIK weight.")]
  22. public float weightSpeed = 1f;
  23. /// <summary>
  24. /// Look the specified target for the specified time.
  25. /// </summary>
  26. public void Look(Transform target, float time) {
  27. if (ik == null) return;
  28. if (ik.solver.IKPositionWeight <= 0f) ik.solver.IKPosition = ik.solver.GetRoot().position + ik.solver.GetRoot().forward * 3f;
  29. lookAtTarget = target;
  30. stopLookTime = time;
  31. }
  32. [HideInInspector] public bool isPaused;
  33. private Transform lookAtTarget; // The target Transform to look at
  34. private float stopLookTime; // Time to start fading out the LookAtIK
  35. private float weight; // Current weight
  36. private bool firstFBBIKSolve; // Has the FBBIK already solved for this frame? In case it is solved more than once, for example when using the ShoulderRotator
  37. public void OnFixTransforms() {
  38. if (ik == null) return;
  39. if (ik.fixTransforms) ik.solver.FixTransforms();
  40. }
  41. public void Update() {
  42. if (ik == null) return;
  43. if (ik.enabled) ik.enabled = false;
  44. if (lookAtTarget == null) return;
  45. if (isPaused) stopLookTime += Time.deltaTime;
  46. // Interpolate the weight
  47. float add = Time.time < stopLookTime? weightSpeed: -weightSpeed;
  48. weight = Mathf.Clamp(weight + add * Time.deltaTime, 0f, 1f);
  49. // Set LookAtIK weight
  50. ik.solver.IKPositionWeight = Interp.Float(weight, InterpolationMode.InOutQuintic);
  51. // Set LookAtIK position
  52. ik.solver.IKPosition = Vector3.Lerp(ik.solver.IKPosition, lookAtTarget.position, lerpSpeed * Time.deltaTime);
  53. // Release the LookAtIK for other tasks once we're weighed out
  54. if (weight <= 0f) lookAtTarget = null;
  55. firstFBBIKSolve = true;
  56. }
  57. public void SolveSpine() {
  58. if (ik == null) return;
  59. if (!firstFBBIKSolve) return;
  60. float headWeight = ik.solver.headWeight;
  61. float eyesWeight = ik.solver.eyesWeight;
  62. ik.solver.headWeight = 0f;
  63. ik.solver.eyesWeight = 0f;
  64. ik.solver.Update();
  65. ik.solver.headWeight = headWeight;
  66. ik.solver.eyesWeight = eyesWeight;
  67. }
  68. public void SolveHead() {
  69. if (ik == null) return;
  70. if (!firstFBBIKSolve) return;
  71. float bodyWeight = ik.solver.bodyWeight;
  72. ik.solver.bodyWeight = 0f;
  73. ik.solver.Update();
  74. ik.solver.bodyWeight = bodyWeight;
  75. firstFBBIKSolve = false;
  76. }
  77. }
  78. }