ControllerState.DampedFloatParameter.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer
  4. {
  5. /// https://kybernetik.com.au/animancer/api/Animancer/ControllerState
  6. partial class ControllerState
  7. {
  8. /************************************************************************************************************************/
  9. /// <summary>
  10. /// A wrapper for <see cref="Mathf.SmoothDamp(float, float, ref float, float, float, float)"/> to control float
  11. /// parameters in <see cref="ControllerState"/>s similar to
  12. /// <see cref="Animator.SetFloat(int, float, float, float)"/>.
  13. /// </summary>
  14. public class DampedFloatParameter
  15. {
  16. /************************************************************************************************************************/
  17. /// <summary>The name of this parameter.</summary>
  18. public ParameterID parameter;
  19. /// <summary>The amount of time allowed to smooth out a value change.</summary>
  20. public float smoothTime;
  21. /// <summary>The last value the parameter was set to.</summary>
  22. public float currentValue;
  23. /// <summary>The value that the parameter is moving towards.</summary>
  24. public float targetValue;
  25. /// <summary>The maximum speed that the current value can move towards the target.</summary>
  26. public float maxSpeed;
  27. /// <summary>The speed at which the value is currently moving.</summary>
  28. public float velocity;
  29. /************************************************************************************************************************/
  30. /// <summary>Creates a new <see cref="DampedFloatParameter"/>.</summary>
  31. public DampedFloatParameter(
  32. ParameterID parameter,
  33. float smoothTime,
  34. float defaultValue = 0,
  35. float maxSpeed = float.PositiveInfinity)
  36. {
  37. this.parameter = parameter;
  38. this.smoothTime = smoothTime;
  39. currentValue = targetValue = defaultValue;
  40. this.maxSpeed = maxSpeed;
  41. }
  42. /************************************************************************************************************************/
  43. /// <summary>Updates the target parameter.</summary>
  44. public void Apply(ControllerState controller)
  45. => Apply(controller, UnityEngine.Time.deltaTime);
  46. /// <summary>Updates the target parameter.</summary>
  47. public void Apply(ControllerState controller, float deltaTime)
  48. {
  49. currentValue = Mathf.SmoothDamp(currentValue, targetValue, ref velocity, smoothTime, maxSpeed, deltaTime);
  50. controller.SetFloat(parameter, currentValue);
  51. }
  52. /************************************************************************************************************************/
  53. }
  54. /************************************************************************************************************************/
  55. }
  56. }